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. JAVAscript学习笔记 js事件 第一节 (原创) 参考js使用表

    <!DOCTYPE html> <html lang="en" onUnload="ud()"> <head> <me ...

  2. CSS系列------选择器和选择器的优先级

    1.1.基本选择器 通配符选择器(*)      通配符选择器的使用方法如下 *{margin:0px; padding:0px;} //*代表所有的 ID选择器(#) ID选择器的使用方式如下: * ...

  3. C#操纵Excel,此工作薄包含嵌入对象,Office 2007的设定方法

    C#操纵Excel,插入OLE对象时报“此工作薄包含嵌入对象,EXCEL可能无法从这些对象中删除个人信息.”, 如网上所述,Office 2003可以通过“菜单>>工具>>选项 ...

  4. 诸葛马前课andoid app 应用

    前段时间学了点安卓开发的知识,也在同时,陪家人看了<新闺蜜时代 >的后面几集,其中,周小北提到了诸葛马前课. 于是网上查了些资料,学习了一下马前课的计算方法,本着程序服务生活的原则,省去不 ...

  5. 如何在ASP.NET Core Web API测试中使用Postman

    使用Postman进行手动测试 如果您是开发人员,测试人员或管理人员,则在构建和使用应用程序时,有时了解各种API方法可能是一个挑战. 使用带有.NET Core的Postman为您的Web API生 ...

  6. javascript获取年月日

    javascript获取年月日代码片段 function getNowDate() { var date = new Date(); var split = "-"; var ye ...

  7. Java多线程学习之线程池源码详解

    0.使用线程池的必要性 在生产环境中,如果为每个任务分配一个线程,会造成许多问题: 线程生命周期的开销非常高.线程的创建和销毁都要付出代价.比如,线程的创建需要时间,延迟处理请求.如果请求的到达率非常 ...

  8. git命令提交项目

    相关的操作命令,总是忘记,故在此记录下: 此为linux下的命, windows的话,去掉sudo即可 1.进入项目代码根目录,执行: sudo git init 把这个目录变成git可以管理的仓库. ...

  9. 件测试博客日记Day03-11.17日 —— 赵天宇 —— 禅道的使用和配置详细版

    说在维基百科先查找关于禅道相关知识,发现有关于禅道这个项目管理软件的详细介绍,然后将相关的介绍进行整理写入文档,在禅道的介绍中也有下载地址并进行安装. (1)软件的基本情况 a.中文名称:禅道项目管理 ...

  10. auxblogcms1.0.6|代码审计

    这周的审计任务,两天前的任务呀~拖延症呀~ 这次审计一个博客----auxblogcms1.0.6,网上也有所记载,我下面会做个总结. axublog是一款php个人博客系统,小巧强大的PHP+MyS ...