hdu 1163 Eddy’s digital Roots

原文 詳見:   http://www.acmerblog.com/hdu-1163-Eddy%27s-digital-Roots-1503.html                                                                                                                                                                                                                            

Eddy’s digital Roots

問題描述 :

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.

輸入:

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 n^n’s digital root on a separate line of the output.

樣例輸入:

2
4
0

樣例輸出:

4
4

 

如果把一個大數的各位數字相加得到一個和,再把這個和的各位數字相加又得一個和,再繼續作數字和,直到最後的數字和是個位數爲止,
這最後的數稱爲最初那個數的“數字根”。這個數字根等於原數除以9的餘數,因此這個計算過程常常稱爲“合九法”。
此題用“合九法”和同餘定理,同於定理爲:
如果兩個乘積除以m的餘數等於這兩個數分別除以m的餘數積。
例如:7%3=1   5%3=2  7*5/3=2=1*2
代碼如下:
01 #include<iostream>
02 using namespace std;
03 int main()
04 {
05     int n,i,s;
06  
07     while(scanf("%d",&n),n)
08     {
09         s=1;
10         for(i=0;i<n;i++)
11         {
12             s=(s*n)%9;
13             if(s==0)
14             {
15                 printf("9/n");
16                 break;
17             }
18         }
19         if(s!=0)printf("%d/n",s);
20     }
21     return 0;
22 }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章