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;
}

最新文章

  1. [LeetCode] Number of 1 Bits 位1的个数
  2. Security3: Schema 和 Permission
  3. 不是SELECTed表达式
  4. Pyhton 单行、多行注释符号使用方法及规范
  5. exec函数族实例解析
  6. java防止脚本注入,通过拦截器实现
  7. 解决PHP开启gd库无效的问题
  8. view,SurfaceView,GLSurfaceView的关系和区别
  9. POJ 1459 Power Network 最大流(Edmonds_Karp算法)
  10. smarty模板引擎原理解析
  11. Web Scale IT 与 6 种 DevOps 工具
  12. Linq——Count、Sum、Min、Max、Average
  13. ES6常用语法整合
  14. C代码通过编译器编译成可执行文件, 需经历 预处理、编译、汇编、链接 四个阶段
  15. spring cloud: 使用consul来替换eureka
  16. 推荐 Net C# 逆向反编译四大工具利器
  17. 魔豆应用开发傻瓜书——helloworld
  18. Knowledge Tracing -- 基于贝叶斯的学生知识点追踪(BKT)
  19. Knockout开发中文API系列3–使用计算属性
  20. USB学习笔记连载(十三):keil的配置环境

热门文章

  1. ThinkPad E431按F1后直接进入系统无法进入BIOS
  2. css水平垂直居中方法(一)
  3. iOS 添加Empty Application模板
  4. (转)嵌入式C开发人员的最好笔试题目
  5. python3-list列表增删改查合并排序
  6. Json Post到 https的坑 - the underlying connection was closed an unexpected error occurred on a send(远程服务器未知错误导致关闭)
  7. kaggle House_Price_XGBoost
  8. com.fasterxml.jackson.databind.JavaType.isReferenceType
  9. C++新标准:列表初始化
  10. delphi xe6 调用java GPS的方法