K - Digital Roots(第二季水)
Description
For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.
Input
Output
Sample Input
Sample Output
#include<iostream>
#include<cmath>
using namespace std;
void f(int a)
{
int s=;
for(int i=;;i++){
int t=pow((double),i);
if(a/t==)break;
s+=a%(*t)/t;
}
if(s/!=)f(s);
else cout<<s<<endl;
}
int main()
{
int n;
while(cin>>n){
if(n==)break;
f(n);
}
return ;
}
所以要用字符串!!
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
void g(int a)
{
int s=;
for(int i=;;i++){
int t=pow((double),i);
if(a/t==)break;
s+=a%(*t)/t;
}
if(s/!=)g(s);
else cout<<s<<endl;
}
void f(string a)
{
int s=,n=a.length();
for(int i=;i<n;i++){
s+=a[i]-'';
}
if(s/!=)g(s);
else cout<<s<<endl;
}
int main()
{
string n;
while(cin>>n){
if(n=="")break;
f(n);
}
//system("pause");
return ;
}
K - Digital Roots(第二季水)的更多相关文章
- F - The Fun Number System(第二季水)
Description In a k bit 2's complement number, where the bits are indexed from 0 to k-1, the weight o ...
- D - Counterfeit Dollar(第二季水)
Description Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are t ...
- I - Long Distance Racing(第二季水)
Description Bessie is training for her next race by running on a path that includes hills so that sh ...
- Y - Design T-Shirt(第二季水)
Description Soon after he decided to design a T-shirt for our Algorithm Board on Free-City BBS, XKA ...
- N - Robot Motion(第二季水)
Description A robot has been programmed to follow the instructions in its path. Instructions for the ...
- S - 骨牌铺方格(第二季水)
Description 在2×n的一个长方形方格中,用一个1× 2的骨牌铺满方格,输入n ,输出铺放方案的总数. 例如n=3时,为2× 3方格,骨牌的铺放方案有三种, ...
- R - 一只小蜜蜂...(第二季水)
Description 有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行.请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数. 其中,蜂房的结构如下所示. ...
- E - Number Sequence(第二季水)
Description A single positive integer i is given. Write a program to find the digit located in the p ...
- G - Reduced ID Numbers(第二季水)
Description T. Chur teaches various groups of students at university U. Every U-student has a unique ...
随机推荐
- Codeforces Round #280 (Div. 2)E Vanya and Field(简单题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 本场题目都比较简单,故只写了E题. E. Vanya and Field Vany ...
- 用ant重新编译jdk加入调试信息
(文章后面提供了ant和build.xml打包之后的build.zip下载,解压build.zip,然后将apache-ant-1.9.2-bin.zip解压即完成了ant安装,也可到http://a ...
- Virtual Environments
virtualenv 再另一篇随笔中已经提到过virtualenv.如果我们有好几个不同的项目,他们需要的第三方包版本不同,那怎么办呢.这时候就需要virtualenv创建一个虚拟环境,里面包含了一个 ...
- CSS3弹性盒模型布局模块介绍
来源:Robert’s talk原文:http://robertnyman.com/2010/12/02/css3-flexible-box-layout-module-aka-flex-box-in ...
- windows下cmd导入与导出mysql 数据库
一.导出数据库 假设导到C:\ordersys.sql 1.CMD下:输入mysqldump -u 用户名 -p 导出的数据库名>c:\ordersys.sql 2.CMD会提示输入密码:输入密 ...
- centos7.0 php-fpm 安装ImageMagic php扩展imagick
Just copy and paste: $ yum install make $ yum install gcc $ yum install php php-fpm php-devel php-pe ...
- [Head First Python]2. BIF(内置函数)
1- range() >>> for each in range(2): print(each) ... 0 1
- python运维开发(二十)----models操作、中间件、缓存、信号、分页
内容目录 select Form标签数据库操作 models操作F/Q models多对多表操作 Django中间件 缓存 信号 分页 select Form标签补充 在上一节中我们可以知道Form标 ...
- linux下安装pdf
官方下载地址:http://www.foxitsoftware.cn/downloads/ 问题:下载官方包以后解压,双击不能打开,也没有任何提示. 用teminal 来打开foxitreader,t ...
- [置顶] SPL讲解(6)--Condition篇
SmartPersistenceLayer 2.0 之 Condition篇 原理 强大的Condition功能是SPL的一个特性,可以使用Condition完成绝大部分的条件定义,使用 ...