ACM. Digital Roots

Background

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.


Example

Input

24
39
0
Output
6
3

code

这题是BUG,绝对的陷阱!乍一看,仿佛蛮简单的嘛,一个递归就可以了(注意,仅在限制整形大小的时候可以用,如果是没有上线大小的数字,绝对不能这么算),代码如下:
 1 #include<iostream>
 2 using namespace std;
 3 unsigned long FindRoot(unsigned long digit)
 4 {
 5   if(digit < 10)
 6     return digit;
 7   unsigned long sumAllUp = 0;
 8   for(;digit;digit /= 10)
 9     sumAllUp += digit%10;
10   return FindRoot(sumAllUp);
11 }
12 int main()
13 {
14   unsigned long digit;
15   while(cin>>digit && digit)
16   {
17     cout<<FindRoot(digit)<<endl;
18   }
19   return 0;
20 }

 

如果考虑到数字有可能查出上线的话,就只能利用类似大数加这样的算法了,代码如下:

 1 #include<iostream>
 2 #include<string>
 3 using namespace std;
 4 int main()
 5 {
 6   string s;
 7   while(cin>>s && s != "0")
 8   {
 9     int digitRoot = 0;
10     for(int s_index = 0; s_index < s.length(); s_index++)
11     {
12       digitRoot += (s[s_index] - '0');
13       if(digitRoot > 9)
14       {
15         digitRoot = digitRoot%10 + digitRoot/10;
16       }
17     }
18     cout<<digitRoot<<endl;
19   }
20   return 0;
21 }

还有一种方法,这种方法需要我们知道digit root是一个整数对9的余数(如果该整数能被9整除,则其digit root是9)。代码如下:

 1 #include <iostream>
 2 #define LEN 5000
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int sum;
 8     char input[LEN]={""};
 9     while(1)
10     {
11         sum=0;
12         cin>>input;
13         for(int i=0;input[i]!='\0';i++)
14         {
15             sum+=(input[i]-'0');
16             input[i]='\0';
17         }
18         if(sum==0)break;
19         if(sum%9==0)cout<<9<<endl;
20         else cout<<sum%9<<endl;
21     }
22     return 0;
23 }

还有些有趣的东西:

1945815日,二战结束,日本宣布投降.把年、月、日的这些数连在一起,就成了1945815.将这些数字重新排列一下,任意构成一个不同的数(比如4591815)在这两个数中,用大的减去小的,得到一个差数.把差的各个数字加起来,如果是二位数,就再把它的两个数字加起来,最后结果是9.(不信,你做一遍)

牛顿出生于16421225日,数学王子高斯出生于1777430日,希尔伯特的生日是1862123日,香港回归的时间是199771日,和上面一样,你将得到四个较大的数,而且按照上面的方法去计算,最后一定也得9.也许有人认为,太奇妙了,自觉地给这些人物,这些事件赋予了神秘色彩,认为"天将降大任于斯人".事实上,您错了.把您的生日写出来做同样的计算,也会有同样的结果.

事实上,用任何一种方法得到一个大数的各位数字相加得到一个和,这个和又是一个新的数,把这个新的数的各位数字相加又得到一个和,如此,重复刚才的过程,只到最后的数字之和是一位数为上.那么这个数就是原数除以9的余数,我们把这个余数称之为原数的"数字根".这个数字根的过程称为"弃九法"

根据同余原理,我们知道,在求一个数的数字根时,可以把原数的数字9舍去,相加得9后,也可以舍去.例如,求549721的数字根时,其中有9,而且5+47+2都是9,尽可以舍去,最后只剩下1,这就是原数的数字根.

由这些知识,我们就能很好地解释前面的9的奥妙了.事实上,一个数,将它的各个数字重排,获得了一个新数.但原数和新数的数字根相同,也就是被9除有相同的余数.把这两个数相减后,又得到一个数.由同余原理知道,这个数就会是9的倍数,它的数字根是09.再经过刚才辗转的过程,再得到一个两位数.事实上,被9整除的两位数的数字之和一定是9,没有例外,这为什么结果总是9的原因.

转载于:https://www.cnblogs.com/QuentinYo/archive/2013/03/31/2991953.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值