Digital Roots
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 25766   Accepted: 8621

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

题意是给出一个数,求每一位的和,如24,就是2+4=6。如果得到的数还是大于等于10的数,就对这个数继续求每一位的和,直到变成个位数。如39>12->3。

不能更标准的递归了。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
using namespace std; string out; void recursion(string test)
{
if(test.length()==1)
{
out=test;
return;
}
int i,result=0;
for(i=0;i<test.length();i++)
{
result += test[i]-'0';
}
int temp;
string test2;
while(result!=0)
{
temp=result%10;
test2 += temp+'0';
result /=10;
}
recursion(test2);
} int main()
{
string test; while(cin>>test)
{
if(test=="0")
break;
recursion(test);
cout<<out<<endl;
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1519:Digital Roots的更多相关文章

  1. 【九度OJ】题目1124:Digital Roots 解题报告

    [九度OJ]题目1124:Digital Roots 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1124 题目描述: T ...

  2. POJ 1284:Primitive Roots(素数原根的个数)

    Primitive Roots Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5709 Accepted: 3261 Descr ...

  3. ACM1013:Digital Roots

    Problem Description The digital root of a positive integer is found by summing the digits of the int ...

  4. 九度OJ 1124:Digital Roots(数根) (递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2963 解决:1066 题目描述: The digital root of a positive integer is found by s ...

  5. POJ 1284:Primitive Roots 求原根的数量

    Primitive Roots Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3381   Accepted: 1980 D ...

  6. 1013:Digital Roots

    注意:大数要用字符串表示! sprintf:字符串格式化命令 主要功能:将格式化的数据写入某个字符串缓冲区 头文件:<stdio.h> 原型 int sprintf( char *buff ...

  7. HDU - 1310 - Digital Roots

    先上题目: Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  8. Digital Roots 分类: HDU 2015-06-19 22:56 13人阅读 评论(0) 收藏

    Digital Roots Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  9. Digital Roots:高精度

    C - Digital Roots Description The digital root of a positive integer is found by summing the digits ...

随机推荐

  1. mysql5.7修改账户密码

    一.首次登录时,修改root账户的密码: vim /etc/my.cnf 在末尾添加 skip-grant-tables ,保存. service mysqld restart 再次登录时,不需要密码 ...

  2. Oracle 修改 提交后 回退

    1. -- 查询你执行update 语句之前的数据 精确到什么时间 select * from 表名 as of timestamp to_timestamp('2017-07-21 17:16:38 ...

  3. mysql 添加索引语句

    1.PRIMARY  KEY(主键索引)        mysql>ALTER  TABLE  `table_name`  ADD  PRIMARY  KEY (  `column`  ) 2. ...

  4. 赶在EW2020之前,FreeRTOS发布V10.3.0,将推出首个LTS版本

    点击下载:FreeRTOSv10.3.0.exe 说明: 1.新版更新: (1)对于IAR For RISC-V进行支持,并且加强了对RISC-V内核芯片支持,做了多处修正. (2)对阿里平头哥CH2 ...

  5. ②java基础——标识符、关键字和基础数据类型

    Java标识符: 1.由英文字母.数字._(下划线)和$组成,长度不限.其中英文字母包含大写字母(A~Z)和小写字母(a~z),数字包含0到9 2.标识符的第一个字符不能是数字(即标识符不能以数字开头 ...

  6. POJ 2287 田忌赛马

    Tian Ji -- The Horse Racing Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 17699   Acc ...

  7. python学习笔记2018-9-17

    1.print("{0:^30}\n{1:^30}\n{1:10}".format("age","name")) {0:^30}中的0是一个 ...

  8. 三十四、在SAP的屏幕选择中,将英文替换成我们想要的文本内容

    一.我们在代码中定义了一个选择屏幕,但是对应的显示界面为英文 界面如下 二.我们选择[转到]-[文本元素] 三.默认的文本内容是问号和三个点 四.我们修改成我们需要的,并激活这个文本,如果不激活会丢失 ...

  9. 156-PHP strrpos和strripos函数

    <?php //定义两个字符串 $str='pasSword'; $position=strrpos($str,'s'); //不区分大小写判断 echo "字母S在{$str}中最后 ...

  10. spark-submit脚本分析

    执行任务 ./spark-submit \ --class cn.com.dtmobile.spark.DebugTest \ --master yarn \ --deploy-mode client ...