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. Java数组去重的方法

    //第一种方式:最开始想到的是利用Set集合的不可重复性进行元素过滤 public static Object[] oneClear(Object[] arr){  Set set = new Has ...

  2. VMware Tools 组件、配置选项和安全要求

  3. 关于AlertDialog多选框中全选和反选的实现办法

    package mobile.android.ch07.multi.choice.dialog; import android.app.Activity; import android.app.Ale ...

  4. Java集合基于JDK1.8的LinkedList源码分析

    上篇我们分析了ArrayList的底层实现,知道了ArrayList底层是基于数组实现的,因此具有查找修改快而插入删除慢的特点.本篇介绍的LinkedList是List接口的另一种实现,它的底层是基于 ...

  5. DateTimePicker控件在WinXP下的BUG

    如图,通过图示的按钮改变datetimepicker的值 ,弹出MessageBox, datetimePicker重新获得焦点后,自动重复点击按钮. 解决办法: new Thread(() => ...

  6. MapReduce On Yarn的执行流程

    1.概述 Yarn是一个资源调度平台,负责为运算程序提供服务器运算资源,相当于一个分布式的操作系统平台,而MapReduce等运算程序则相当于运行于操作系统之上的应用程序. Yarn的架构如下图所示: ...

  7. tomcat端口号被占用,且杀进程不能够杀掉解决办法

    在电脑上安装了zookeeper以后,配置好tomcat启动发现端口号8009端口号被占用,报错如下: 采用netstat –ano 查询所有进程查看或者根据端口号查进程netstat -ano |f ...

  8. 计算机操作系统学习(一) Linux常用指令(随时更新)

    1.chmod 以下转载至https://blog.csdn.net/summer_sy/article/details/70142475 chmod u+x file.sh 就表示对当前目录下的fi ...

  9. Python 实现类似range函数

    需求:写一个属于你自己的 frange函数,frange与range类似,一样的参数规则,但是每一项必须要是float类型 实现: 注意点,如何判断stop是否有参数传入,这里使用空字符判断,如fra ...

  10. 竟然把Ruoyi在我自己的Eclipse编译成功,并能跑通了。。。。服了我自己了

    前几天,下载最新ECLISPSE2019压缩包,解压缩成功,没提示不是免费:eclipse-jee-2019-12-R-win32-x86_64.zip然后我配置好了maven于是我1月2日晚一时兴起 ...