数论之数字根 杭电1013

来源:互联网 发布:世界银行预测数据 编辑:程序博客网 时间:2024/05/22 15:44

       做这道题就有一种感觉,,数学真是奇妙,,在网上查了一下,才知道数字根有那么多奇妙的性质。不过,对于这道题我却是不太理解,,主要是不会证明为什么数字根就是各个位加起来对9取余,,我试着用同余证了一下,,可惜,,没证出来,,还希望高手指点。题目:

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
24390
 


 

Sample Output
63

 

ac代码:

#include <iostream>#include <cstdio>#include <string>#include <string.h>using namespace std;int main(){  char ch[10005];  int sum=0;  while(cin>>ch&&ch[0]!='0')  {    int len=strlen(ch);      sum=0;  for(int i=0;i<len;++i)  {    sum+=(ch[i]-'0');  } if(sum%9==0) printf("9\n"); elseprintf("%d\n",sum%9);  }  return 0;} 


 

原创粉丝点击