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. Three ways to throw exception in C#. Which is your preference?

    There are three ways to 'throw' a exception in C#  C#中有三种抛出异常的方式 Use the throw keyword without an id ...

  2. HTML5对音视频的处理

      前  言 现在网上有许多的框架和插件,能够满足程序猿的各种需求,慢慢的,就有些忽视最基础的东西. 比如,大多数视频是通过插件(比如 Flash)来显示的.然而,并非所有浏览器都拥有同样的插件. H ...

  3. linux操作系统基础篇(四)

    一.系统监控 1.TOP 命令 1) top命令的功能:top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器. 2) 使用top命令列 ...

  4. TCollector

    TCollector tcollector is a client-side process that gathers data from local collectors and pushes th ...

  5. 在64位Win7环境+64位JDK下,运行64位Eclipse,提示“Failed to load the JNI shared library”错误,提示jvm.dll不对

    -startup plugins/org.eclipse.equinox.launcher_1.2.0.v20110502.jar --launcher.library plugins/org.ecl ...

  6. 一般处理程序获取Session方式

    今天写程序得时候遇到了一个问题:ajax在对ashx进行请求时如果按照 context.Request方式直接来获取值得话获取到得是空值,因此去网上搜了一下问题.现记录如下: ashx获取sessio ...

  7. android堆栈调试--详细

    1.将ndk中的arm-linux-androideabi-addr2line可执行文件的路径加入配置文件~/.bashrc中,例如: export PATH=$PATH:~/dlna/android ...

  8. input type="radio" 赋值问题

    之前项目中 后台传值 然后赋给单选input   在网上找了好久,现在有时间了,整理一下  ,方便以后有人会用到. $('radio[name="sex"][value'" ...

  9. webpack 1.x 学习总结

    webpack介绍(from github): A bundler for javascript and friends. Packs many modules into a few bundled ...

  10. [面试没答上的问题1]http请求,请求头和响应头都有什么信息?

    最近在找工作,面试官问了一些问题自己并没有回答上,这里做一个小结. http请求,请求头和响应头都有什么信息? 页面和服务器交互最常见的方式就是ajax,ajax简单来说是浏览器发送请求到服务端,然后 ...