Digital Roots

短命女 2022-08-10 04:44 131阅读 0赞

Digital Roots

Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^

题目描述

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.

输入

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.

输出

For each integer in the input, output its digital root on a separate line of the output.

示例输入

  1. 24
  2. 39
  3. 0

示例输出

  1. 6
  2. 3

提示

请注意,输入数据长度不超过20位数字。

来源

Greater New York 2000
面向对数据结构和算法不是太懂的同学

示例程序

  1. #include<stdio.h>
  2. #include<string.h>
  3. int main()
  4. {
  5. int i,j,n,m,b;
  6. char a[30];
  7. while(scanf("%s",a)&&a[0]!='0')
  8. {
  9. m=0;
  10. n=strlen(a);
  11. for(i=0;i<n;i++)
  12. {
  13. m+=a[i]-'0';
  14. }
  15. while(m>9)
  16. {
  17. b=0;
  18. while(m>=1)
  19. {
  20. b+=m%10;
  21. m=m/10;
  22. }
  23. m=b;
  24. }
  25. printf("%d\n",m);
  26. }
  27. }

发表评论

表情:
评论列表 (有 0 条评论,131人围观)

还没有评论,来说两句吧...

相关阅读

    相关 HDU-1163 Eddy's digital Roots

    前置芝士: 同余定理,快速幂 这个题目是杭电上的一道数论相关的题目 如果暴力的话是一定会超时的,这个时候我们就要用到同余定理来优化。 同余定理是数论中的重要概念,下面贴

    相关 Digits

    \\(Digits\\) ![m6C1AO.png][] 这道题目比较简单,首先先打出来暴力,然后一看\\(b\\)的范围,瞬间想到快速幂。 快速幂的精髓是什么?