Digital root------[NWPU][2018寒假作业][通用版]一、热身 [Cloned]M题//求数字根的简便方法
2018-01-22 22:55
393 查看
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
解题思路:
因为题目中对输入的数据没有限制,可能很大,所以应该用字符串类型输入。
对于求数字根的问题,有一个简便快速的方法,将一个数的所有数位加和模9,结果就是这个数的数字根,对于9的倍数模9为0这种特殊情况,数字根记为9。
n%9?n%9:9;
可以用这个语句代替下面代码中root的计算,更为简便。
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
解题思路:
因为题目中对输入的数据没有限制,可能很大,所以应该用字符串类型输入。
对于求数字根的问题,有一个简便快速的方法,将一个数的所有数位加和模9,结果就是这个数的数字根,对于9的倍数模9为0这种特殊情况,数字根记为9。
n%9?n%9:9;
可以用这个语句代替下面代码中root的计算,更为简便。
#include<stdio.h> #include<string.h> //求数字根mod9即可 int main() { int n,i; int root; char num[10000]; while(1) { scanf("%s",num); if(strlen(num)==1&&num[0]=='0') { return 0; } for(n=0,i=0;i<strlen(num);i++) { n+=num[i]-'0'; } if(n%9==0) { root=9; } else { root=n%9; } printf("%d\n",root); } }
相关文章推荐
- Joseph------[NWPU][2018寒假作业][通用版]一、热身 [Cloned]R题
- Dirichlet's Theorem on Arithmetic Progressions------[NWPU][2018寒假作业][通用版]一、热身 [Cloned]O题
- Ignatius and the Princess IV ------[NWPU][2018寒假作业][通用版]一、热身 [Cloned]J题
- HDU2054 A==B------ [NWPU][2018寒假作业][通用版]一、热身 [Cloned]Y题
- A + B ------[NWPU][2018寒假作业][通用版]一、热身 [Cloned]E题
- Candy Sharing Game------[NWPU][2018寒假作业][通用版]一、热身 [Cloned]B题
- stl的map1------[NWPU][2018寒假作业][通用版]二、stl ,模拟,贪心等 [Cloned]A题
- //switch结构//Edge------[NWPU][2018寒假作业][通用版]一C题
- Doing Homework Again贪心------[NWPU][2018寒假作业][通用版]二、stl ,模拟,贪心等 [Cloned]P题
- stl的map2------[NWPU][2018寒假作业][通用版]二、stl ,模拟,贪心等 [Cloned]B题
- Robot Motion------[NWPU][2018寒假作业][通用版]一F题
- Hero贪心2------[NWPU][2018寒假作业][通用版]二Q题
- //搞不清楚for循环真的害死人//贪心FatMouse's Trade------[NWPU][2018寒假作业][通用版]二E题
- //动能定理和弹性势能//James Bond------[NWPU][2018寒假作业][通用版]二D题
- //为什么一直PE?//经营水果商店------[NWPU][2018寒假作业][通用版]二C题
- //递推//折线分割平面------[NWPU][2018寒假作业][通用版]二F题
- 2018寒假作业2
- 2018寒假作业4
- Latex输入罗马数字的最简便方法
- 2018寒假作业_2(电梯版本一)