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. win10提示防火墙没有法更改某些设置的处理办法

    一.问题发现 远程链接电脑时间发现远程链接失败 提问在“控制面板” 中打开“程序” 列表中启用“windows 防火墙” . 按照提示启用防火墙 ,发现启用或关闭页面不可编辑 二.原因是防火墙Wind ...

  2. JDK8~JDK11的新特性

    #JDK 1.8 新特性接口中的静态方法 只能由接口自己调用 接口中的默认方法 可以不被覆盖 #JDK 1.9 新特性(可能在JDK8中被忽略了,没来得及加)接口可以定义私有方法,但是只能让自己调用, ...

  3. python爬虫破解带有RSA.js的RSA加密数据的反爬机制

    前言 同上一篇的aes加密一样,也是偶然发现这个rsa加密的,目标网站我就不说了,保密. 当我发现这个网站是ajax加载时: 我已经习以为常,正在进行爬取时,发现返回为空,我开始用findler抓包, ...

  4. win2008R2 局域网共享

    1.解决windows server 2008 R2 不能开启网络发现 开始–>管理工具–>服务,以下3个服务选择自动.开启: Function Discovery Resource Pu ...

  5. Delphi IDFtp用法,包含断点续传

    1  连接远程服务器procedure Connect(AAutoLogin: boolean; const ATimeout: Integer);2  改变目录procedure ChangeDir ...

  6. NFS挂载共享文件夹

    修改rcS启动脚本,使开发板初始化完成,自动挂载共享文件夹 修改开发板ip,使之与虚拟机处于同一网段(二者可以互ping)     挂载虚拟机的共享文件夹     rcS 1 ifconfig eth ...

  7. centos7安装google-chrome和chromedriver

    1.root用户下进入到etc/yum.repos.d目录下  [root@f7d6b9f2-1291-4d2f-8805-aef94deac9f7 yum.repos.d]# pwd  /etc/y ...

  8. [BJDCTF2020]Cookie is so stable

    0x00 知识点 Twig模板注入 链接: https://www.k0rz3n.com/2018/11/12/%E4%B8%80%E7%AF%87%E6%96%87%E7%AB%A0%E5%B8%A ...

  9. springBoot (适合ssm)

    很多小白在学ssm的时候,选用idea,网上应该有很多教程,创建maven项目,创建spring项目的都有,五花八门. 最近接触了springBoot,这个项目类型适用于ssm,还不用去创建很多文件夹 ...

  10. Wordpress自动更新失败

    Wordpress自动更新失败 一.手动升级Wordpress教程第一步:备份好Wordpress数据 在决定手动升级Wordpress前,你需要做好Wordpress的数据备份工作,以防手动升级失败 ...