Eddy's digital Roots

Eddy's digital Roots

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 12   Accepted Submission(s) : 9
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. The Eddy's easy problem is that : give you the n,want you to find the n^n's digital Roots.
 

 

Input
The input file will contain a list of positive integers n, one per line. The end of the input will be indicated by an integer value of zero. Notice:For each integer in the input n(n<10000).
 

 

Output
Output n^n's digital root on a separate line of the output.
 

 

Sample Input
2
4
0
 

 

Sample Output
4
4
题目大意:
  输入一个数N,然后让你求N*N的树根为多少。
  根据题目的意思,树根的定义为:计算该数的各位和,如果累加和大于等于10,则继续计算累加和的各位和、
  比如,用a,b表示一个数,则用ab表示a*10+b。
    计算ab的树根,计算可得ab的各位累加和为(a+b),如果(a+b)>=10,则继续计算累加和(a+b)的各位和、直到累加和小于10。(弃九法:对9取余即可)。
  但是题目所要求的是N*N的树根。所以,还需要进一步探索;
  比如N位两位数时:
    ab*ab = (a*10+b)*(a*10+b) = 100*a*a+10*2*a*b+b*b
    =》则ab*ab的第一次累加和为a*a+2*a*b+b*b = (a+b)*(a+b)------①
    =》所以,求ab*ab的树根转换为对((a+b)*(a+b))%9;
  
    
    ab*cd = (a*10+b)*(c*10+d) = 100*a*c+10*(a*d+b*c)+b*d-----②
    =》则ab*cd的第一次累加和为a*c+(a*d+b*c)+b*d = (a+b)*(c+d)
    =》所以,求ab*ab的树根转换为对((a+b)*(c+d))%9;-----------(结论)
 
  结合①和②可推导出:ab*ab*ab....ab的树根为((a+b)*(a+b)...*(a+b))%9;
  再者若N为三位数时:

    abc*abc=(100*a+10*b+c)*(100*a+10*b+c)

    =10000*a*a+2000*a*b+100*b*b+200*a*c+20*b*c+c*c

    =a*a+2*a*b+b*b+2*a*c+2*b*c+c*c

    =(a+b)^2+2*c*(a+b)+c*c

    = (a+b+c)*(a+b+c)---------③

  由③可知,即便N为多位数的话abcde*abcde=(a+b+c+d+e)*(a+b+c+d+e)也能够成立、

  同理结合①和②,可以知,对于多位数的N,结论仍然成立.故可以推广到多位数:

  详细代码如下:

 1 #include<stdio.h>
 2 #include<string.h>
 3 int main()
 4 {
 5     int NUM,i,sum;
 6     while(scanf("%d",&NUM)!=EOF&&NUM)
 7     {
 8         for(i=1,sum=1;i<=NUM;i++)
 9             sum=(sum*(NUM%9))%9;    /*把每次结果取余9*/
10         if(sum==0)printf("9\n");
11         else printf("%d\n",sum);    /*输出9余数*/
12     }
13     return 0;
14 }
View Code

PS:之前理解错了= -,真不好意思,还好有及时发现(一年后...囧囧囧...)、、、、

参考博客:

    http://blog.sina.com.cn/s/blog_91e2390c0101414y.html

    http://blog.csdn.net/iamskying/article/details/4738838

 

posted @ 2014-08-22 13:16  Wurq  阅读(121)  评论(0编辑  收藏  举报