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. linux中df和du查看磁盘大小不一致解决方法

    挂了一块50G到/data目录下#  df -h Filesystem Size Used Avail Use% Mounted on /dev/xvdb1 50G 46G 1.2G 98% /dat ...

  2. kali linux 添加源 及为firefox esr 添加 flash 插件 --2017

    终端 输入 vim /etc/apt/sources.list 在文件内写入 #中科大deb http://mirrors.ustc.edu.cn/kali kali-rolling main non ...

  3. Python爬虫一爬取B站小视频源码

    如果要爬取多页的话 在最下方循环中 填写好循环的次数就可以了 项目源码 from fake_useragent import UserAgent import requests import time ...

  4. eshop7-mysql

    1. Mysql 安装 执行 yum -y install mysql-server 注意:(1)是否使用sudo 权限执行请根据您具体环境来决定 (2)检查是否已经安装mysql-server rp ...

  5. 使用WinDbg分析蓝屏dump原因

    大多数人或许都经历过系统蓝屏问题,然而大多数人不清楚该怎么处理蓝屏问题,这里主要对系统蓝屏做一些解释,同时介绍下蓝屏问题分析工具WinDbg分析蓝屏问题的一般步骤. 微软官方对蓝屏的定义是,当系统遇到 ...

  6. 安卓Recycleview简单的网格布局-初学者的关键点

    导包 def supportVersion = '28.0.0' 定义常量我的sdk版本为28 implementation "com.android.support:recyclervie ...

  7. 吴裕雄--天生自然java开发常用类库学习笔记:大数操作

    import java.math.* ; class MyMath{ public static double add(double d1,double d2){ // 进行加法计算 BigDecim ...

  8. Web基础之Spring MVC

    Spring MVC Spring MVC 说是框架,对Tomcat来说其实就是一个Servlet,关于如何从上古时期的Servlet演化到如今的SpringMVC的,可以看看这篇博文:Spring ...

  9. Golang的基础数据类型-浮点型

    Golang的基础数据类型-浮点型 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.浮点型概述 Go语言提供两种精度的浮点数,即float32和float64,其中float32 ...

  10. eclipse 安装spring tools suite插件

    之前使用idea进行springboot项目的开发学习,但是由于idea是收费的,总是用着用着说验证码到期之类的,总之还是很不爽,于是就想重新采用eclipse开发springboot项目,为了方便s ...