九余数定理
来源:互联网 发布:用python做大数据分析 编辑:程序博客网 时间:2024/06/06 03:56
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.
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
24390
Sample Output
63
题意分析:数根的定义:将一个数的每位数相加得到一个新的数,如果该数为一位数,该新数即是数根;否则重复开头的步骤。例如:28945 -> 2+8+9+4+5=28 -> 2+8+10 -> 1+0 = 1;所以1即是28945的数根。
解题思路:当然,直接按题目的字面意思做也能AC。这里我只是想介绍一下九余数定理。
我么们知道,不管是什么数,数根一定是0~9其中的一个,因为不会有正数各位之和为0。
证明:
假设,数d的根为d%9( 暂时不取0,整除时取9)
当d < 10时,1~9这9个数肯定成立;
当d >= 10时,d的根为d%9 = (d-1)%9+1,即d的前一个数的数根加1.
得证.
假设,数d的根为d%9( 暂时不取0,整除时取9)
当d < 10时,1~9这9个数肯定成立;
当d >= 10时,d的根为d%9 = (d-1)%9+1,即d的前一个数的数根加1.
得证.
源代码如下:
<strong>#include<cstdio>int main(void){while(1){ int num=0; char ch;while(scanf("%c",&ch) && ch!='\n') num += ch-'0';if(!num) return 0;else if(num%9 == 0) puts("9");else printf("%d\n",num%9);}return 0;}</strong>
类似的题:
Eddy's digital Roots 只不过这里变成求Nn的数根
源代码如下:
<span style="font-size:18px;">#include<cstdio>int main(void){ int n,i,ans; while(scanf("%d",&n),n) { ans = 1; for(i = 0; i < n; i++) ans = ans*n%9; if(!ans) puts("9"); else printf("%d\n",ans); } return 0;}</span>
0 0
- 九余数定理
- 九余数定理
- 九余数定理
- 九余数定理
- 九余数定理
- HDU1163九余数定理
- 九余数定理扩展
- HDU1163【九余数定理】【水题】
- HDU-1013九余数定理
- nyoj 485 九余数定理
- nyoj 424 && 485(九余数定理)
- 九余数定理及证明 hdu 1163
- hdu 1163(九余数定理)
- hdu1013 Digital Roots (模拟||九余数定理)
- 九余数定理(hdu-P1163)
- hdu 1013 Digital Roots (九余数定理)
- 求a+b及九余数定理
- hdu1163Eddy's digital Roots(九余数定理+同余定理)
- contiki 节约内存的方法
- C#面向对象(四)接口
- enum枚举类型
- 浙大 PAT b1028
- Enactus(创行)介绍
- 九余数定理
- eCryptfs源码注释(3)
- 浙大 PAT b1029
- [webGL学习]基于three.js构建WebGL实例第三讲
- Android 四大组件(Activity、Service、BroadCastReceiver、ContentProvider)
- memcpy与strcpy的区别
- Ajax快速入门
- 内网上不了Github的解决方案
- MAC 系统下mysql报错(ERROR 1045 (28000))