Digital Roots

Problem 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

一个数。各个位数相加得到的数假设小于10就输出,否则就继续把得到的数各个位数相加。一看我就模拟做的。模拟的时候要注意。输入的数字可能非常大。所以用int是不能够的,要用字符串处理。模拟做法代码例如以下:

#include<cstdio>
#include<cstring>
void zuo(int x){
int sum=0;
while(x){
sum+=(x%10);
x/=10;
}
if(sum<10) printf("%d\n",sum);
else zuo(sum);
}
int main(){
char s[1010];
while(scanf("%s",s)&&s[0]!='0'){
int x=0;
for(int i=0;i<strlen(s);i++)
x+=(s[i]-'0');
zuo(x);
}
return 0;
}

另一种解法。数论的知识。

数字本身:     1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  12  22  23  24  25  26  27  28  29  30············

各个位数和:  1  2  3  4  5  6  7  8  9   1   2    3     4    5    6    7    8    9    1    2    3    4    5    6   7    8    9    1    2    3·············

你会发现。每9个是一个循环。所以仅仅要对9取余就ok了。代码例如以下:

#include<cstdio>
#include<cstring>
int main(){
char s[1010];
while(scanf("%s",s)&&s[0]!='0'){
int x=0;
for(int i=0;i<strlen(s);i++)
x+=(s[i]-'0');
x=x%9;
if(x==0) printf("9\n");
else printf("%d\n",x);
}
return 0;
}

HDU 1013.Digital Roots【模拟或数论】【8月16】的更多相关文章

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

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

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

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

  3. HDU 1013 Digital Roots(字符串)

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

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

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

  5. HDU 1013 Digital Roots 题解

    Problem Description The digital root of a positive integer is found by summing the digits of the int ...

  6. hdu 1013 Digital Roots

    #include <stdio.h> int main(void) { int m,i;char n[10000]; while(scanf("%s",&n)= ...

  7. HDU OJ Digital Roots 题目1013

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

  8. HDU - 1310 - Digital Roots

    先上题目: Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  9. 杭电 1013 Digital Roots

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1013 反思:思路很简单,但是注意各位数加起来等于10的情况以及输入0的时候结束程序该怎么去表达 #in ...

随机推荐

  1. ThreadLocal 原理解析

    1.对Thread local 理解 ThreadLocal 是为了解决线程间同步而创建的一个新的思路.简单来说就是每个线程都保存一个变量副本. 如果在Thread 内部定义一个field变量,也可以 ...

  2. 非确定性计算引擎转化为C#版本并重构

    这是之前我写的原始的 VB.NET 版本: http://www.cnblogs.com/RChen/archive/2010/05/17/1737587.html 转化为 C# 版本后,还进行了一些 ...

  3. 谈谈我的移动端rem适配方案

    最近有点怀疑人生,毕竟一个人写前端,有时候会怀疑自己理解的一些东西包括用法有没有符合标准.趁着这阵子闲下来,翻了翻别人的rem适配博客,发现有点绕口,怪自己是个强迫症,啥都要自己去试试结果并从中理解, ...

  4. Python基础---python中的异常处理

    Python中的异常处理 一.什么是异常处理 python解释器检测到错误,触发异常(也允许程序员自己触发异常) 程序员编写特定的代码,专门用来捕捉这个异常(这段代码与程序逻辑无关,与异常处理有关) ...

  5. 如何基于 eolinker 的进行接口管理

    由于工作的原因,经常要接触到很多API接口,而API接口在设计时往往需要编写大量的文档,而且编写完成后往往需要根据实际情况,经常改动文档,这使得文档编写维护工作量相对较大,这让我也包括很多的开发者都很 ...

  6. 《Linux命令行与shell脚本编程大全》第十二章 使用结构化命令

    许多程序要就对shell脚本中的命令施加一些逻辑控制流程. 结构化命令允许你改变程序执行的顺序.不一定是依次进行的 12.1 使用if-then语句 如下格式: if command then     ...

  7. cmd+任务管理器解决端口被占用的问题

    1.打开cmd命令行 2.输入命令netstat –ano,会显示所有被占用的端口号以及占用该端口的程序所对应的进程号. (local address下面是端口号,PID是占用端口的某程序的进程号) ...

  8. 泛型里的super和extend

    <? extends T>和<? super T>应该怎么用? 网上看到一些比较难懂的回答,但是在EffectiveJava(2th Edition)遇到简单明了的解释: If ...

  9. while循环小练习-猜答案

    条件 1.每个用户只能猜10次产品的价格2.每次猜玩价格,提示用户价格是多了还是少了或者对了3.如果用户才对则终止程序执行 break! i = 0 #设置一个次数变量 price = 38 #设置一 ...

  10. Python案例分享

    1.过桥(爬金字塔): 1 i = 1 2 while i <= 9: 3 if i < 6: 4 j = 0 5 while j < i: 6 print('*',end=' ') ...