Description

The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as necessary to obtain a single digit.        
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

The input file will contain a list of positive integers, one per line. The end of the input will be indicated by an integer value of zero.        
                

Output

For each integer in the input, output its digital root on a separate line of the output.        
                

Sample Input

24
39
0
                

Sample Output

6
3
 
 
用int 的话  会 结果错误!!
#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(第二季水)的更多相关文章

  1. 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 ...

  2. D - Counterfeit Dollar(第二季水)

    Description Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are t ...

  3. I - Long Distance Racing(第二季水)

    Description Bessie is training for her next race by running on a path that includes hills so that sh ...

  4. Y - Design T-Shirt(第二季水)

    Description Soon after he decided to design a T-shirt for our Algorithm Board on Free-City BBS, XKA ...

  5. N - Robot Motion(第二季水)

    Description A robot has been programmed to follow the instructions in its path. Instructions for the ...

  6. S - 骨牌铺方格(第二季水)

    Description          在2×n的一个长方形方格中,用一个1× 2的骨牌铺满方格,输入n ,输出铺放方案的总数.         例如n=3时,为2× 3方格,骨牌的铺放方案有三种, ...

  7. R - 一只小蜜蜂...(第二季水)

    Description          有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行.请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数.         其中,蜂房的结构如下所示.     ...

  8. E - Number Sequence(第二季水)

    Description A single positive integer i is given. Write a program to find the digit located in the p ...

  9. G - Reduced ID Numbers(第二季水)

    Description T. Chur teaches various groups of students at university U. Every U-student has a unique ...

随机推荐

  1. Codeforces Round #280 (Div. 2)E Vanya and Field(简单题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 本场题目都比较简单,故只写了E题. E. Vanya and Field Vany ...

  2. 用ant重新编译jdk加入调试信息

    (文章后面提供了ant和build.xml打包之后的build.zip下载,解压build.zip,然后将apache-ant-1.9.2-bin.zip解压即完成了ant安装,也可到http://a ...

  3. Virtual Environments

    virtualenv 再另一篇随笔中已经提到过virtualenv.如果我们有好几个不同的项目,他们需要的第三方包版本不同,那怎么办呢.这时候就需要virtualenv创建一个虚拟环境,里面包含了一个 ...

  4. CSS3弹性盒模型布局模块介绍

    来源:Robert’s talk原文:http://robertnyman.com/2010/12/02/css3-flexible-box-layout-module-aka-flex-box-in ...

  5. windows下cmd导入与导出mysql 数据库

    一.导出数据库 假设导到C:\ordersys.sql 1.CMD下:输入mysqldump -u 用户名 -p 导出的数据库名>c:\ordersys.sql 2.CMD会提示输入密码:输入密 ...

  6. 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 ...

  7. [Head First Python]2. BIF(内置函数)

    1- range() >>> for each in range(2): print(each) ... 0 1

  8. python运维开发(二十)----models操作、中间件、缓存、信号、分页

    内容目录 select Form标签数据库操作 models操作F/Q models多对多表操作 Django中间件 缓存 信号 分页 select Form标签补充 在上一节中我们可以知道Form标 ...

  9. linux下安装pdf

    官方下载地址:http://www.foxitsoftware.cn/downloads/ 问题:下载官方包以后解压,双击不能打开,也没有任何提示. 用teminal 来打开foxitreader,t ...

  10. [置顶] SPL讲解(6)--Condition篇

    SmartPersistenceLayer 2.0 之 Condition篇 原理        强大的Condition功能是SPL的一个特性,可以使用Condition完成绝大部分的条件定义,使用 ...