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. chart 模板【转】

    Helm 通过模板创建 Kubernetes 能够理解的 YAML 格式的资源配置文件,我们将通过例子来学习如何使用模板. 以 templates/secrets.yaml 为例: 从结构看,文件的内 ...

  2. 关于 float 型和 double 型的区别,以及 char 型和 String 型的不同

    一.1.float是单精度浮点数,内存分配4个字节,占32位,有效小数位6-7位 double是双精度浮点数,内存分配8个字节,占64位,有效小数位15位 2.java中默认声明的小数是double类 ...

  3. Day5 - G - The Unique MST POJ - 1679

    Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spann ...

  4. Java程序员学习Go指南(三)

    转载:https://www.luozhiyun.com/archives/213 人是否会进步以及进步得有多快,依赖的恰恰就是对自我的否定,这包括否定的深刻与否,以及否定自我的频率如何.这其实就是& ...

  5. 005.CI4框架CodeIgniter, 通过带路径的view视图访问

    01. 我们在app目录的Views文件夹中新建一个Login文件,再创建一个login.php文件,在里面我们写上如下代码: <!doctype html> <html> & ...

  6. 《YouTube 网站的架构演进》阅读笔记

    概述 YouTube 在国内是个404网站,需要翻墙得见,这是有用的废话,先铺垫一下. 从全球网站来看,它仅次于母公司 Google,全球排名位列第2.每天超过5亿以上视频播放量,平均每个用户点击10 ...

  7. ES6学习笔记-扩展运算符(...)

    扩展运算符的定义: es6中引入扩展运算符(...),它用于把一个数组转化为用逗号分隔的参数序列. 它常用在不定参数个数时的函数调用,数组合并等情形. 用法一:不定参数个数时的函数调用 <scr ...

  8. SVPWM总结

    空间矢量算法 是以逆变器和电机作为一个整体来研究的.目标是产生电机定子的圆形磁场 模态选择, 上管导通 状态为1 下管导通 状态为0 那么状态为000 001 010 011 100 101 110 ...

  9. 在 CentOS 中部署 KMS 服务器(vlmcsd)

    准备 vlmcsd 下载 vlmcsd 本文使用的 vlmcsd 版本为 svn1111,支持的产品: Windows Vista – 10Windows Server 2008 - 2016Offi ...

  10. Delphi 10.4 最新消息

    官方发布了关于10.4的消息,译文如下: 做为我们的Delphi,C ++ Builder和RAD Studio的订阅客户,除了获得更新,升级和技术支持等主要好处外,我们还邀请订阅客户参加Beta计划 ...