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

思路:循环求解,但要考虑输入的数特别大时,就要考虑字符串模拟大数的输入了

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream> using namespace std;
int sum;
string n;
int root(int x) {
sum=0;
while(x) {
sum+=x%10;
x/=10;
}
return sum;
} int main() { while(cin>>n) {
if(n=="0") {
break;
}
int s=0;
for(int t=0; t<n.length(); t++) {
s+=n[t]-'0';
} if(s>=10) {
while(root(s)>=10) {
s=sum;
}
} else {
sum=s;
}
cout<<sum<<endl;
} return 0;
}

HDU-Digital Roots(思维+大数字符串模拟)的更多相关文章

  1. HDU 1013 Digital Roots(字符串,大数,九余数定理)

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

  2. HDU 1013.Digital Roots【模拟或数论】【8月16】

    Digital Roots Problem Description The digital root of a positive integer is found by summing the dig ...

  3. HDU 1013 Digital Roots【字符串,水】

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

  4. HDU 1013 Digital Roots(字符串)

    Digital Roots Problem Description The digital root of a positive integer is found by summing the dig ...

  5. HDU 1013 Digital Roots(to_string的具体运用)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1013 Digital Roots Time Limit: 2000/1000 MS (Java/Othe ...

  6. 用字符串模拟两个大数相加——java实现

    问题: 大数相加不能直接使用基本的int类型,因为int可以表示的整数有限,不能满足大数的要求.可以使用字符串来表示大数,模拟大数相加的过程. 思路: 1.反转两个字符串,便于从低位到高位相加和最高位 ...

  7. 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 ...

  8. hdu 2629 Identity Card (字符串解析模拟题)

    这题是一个字符串模拟水题,给12级学弟学妹们找找自信的,嘿嘿; 题目意思就是要你讲身份证的上的省份和生日解析出来输出就可以了: http://acm.hdu.edu.cn/showproblem.ph ...

  9. HDU-1002.大数相加(字符串模拟)

    本题大意:给出两个1000位以内的大数a 和b,让你计算a + b的值. 本题思路:字符串模拟就能过,会Java的大佬应该不会点进来...... 参考代码: #include <cstdio&g ...

随机推荐

  1. struts2学习笔记(5)拦截器

    继承AbstractInterceptor类,在类中完成拦截器的功能,只需实现intercept方法即可,提供了init()和destroy()的空实现 示例:显示执行action所用的时间 ①在sr ...

  2. OpenCV4Android 不需要安装OpenCVManager,就可以运行的方法

    http://blog.csdn.net/yanzi1225627/article/details/27863615 OpenCV4Android释疑: 透析Android以JNI调OpenCV的三种 ...

  3. C++中cin.get(),cin.getline(),cin>>,gets(),cin.clear()使用总结

    1.cin.get()  实质:类istream所定义对象cin的重载成员函数 用于读取单字符  istream& get(char&)    int get(void) 用于读取字符 ...

  4. DB2--sql计算时间差和格式化时间

    格式化时间 db2 格式化时间使用的 TO_CHAR(TIMESTAMP('2017-10-24 21:18:12'),'YYYY-MM-DD'): 测试sql:SELECT TO_CHAR(TIME ...

  5. JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-007Inheritance of embeddable classes(@MappedSuperclass、@Embeddable、@AttributeOverrides、、)

    一.结构 二.代码 1. package org.jpwh.model.inheritance.embeddable; import javax.persistence.MappedSuperclas ...

  6. jQuery的事件处理方法

    .on()方法用来处理事件 $("li").on("click",function(){ $(this).addClass("complete&quo ...

  7. Vue国际化的使用

    首先是是在main.js文件中把国际化引入进来 import Vue from 'vue' import App from './App' import router from './router' ...

  8. SQL CTE 递归 查询省,市,区

    IF OBJECT_ID('tb') IS NOT NULL DROP TABLE tb ) , pid ) , name )) ' , null , '广东省') ' , '广州市') ' , '深 ...

  9. 【Python发展】pandas和koalas

    1.pandas介绍 Python 数据科学在过去几年中爆炸式增长, pandas 已成为生态系统的关键.当数据科学家得到一个数据集时,他们会使用 pandas 进行探索.它是数据处理和分析的终极工具 ...

  10. ThinkPHP3.2.3完整版创建前后台入口文件 http://jingyan.baidu.com/article/7e4409533fc1092fc1e2ef53.html

    ThinkPHP3.2.3完整版创建前后台入口文件   1 2 3 4 5 6 7 分步阅读 ThinkPHP是为了简化企业级应用开发和敏捷WEB应用开发而诞生的优秀的国产php框架,值得我们去探索学 ...