HDOJ--1013Digital Roots

原题链接


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.
 

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
 
   
24 39 0
 

Sample Output
 
   
6 3
 

数根(又称 数字根Digital root)是自然数的一种性质,换句话说,每个自然数都有一个 数根
数根是将一正整数的各个位数相加(即横向相加),若加完后的值大于等于10的话,则继续将各位数进行横向相加直到其值小于十为止,或是,将一数字重复做数字和,直到其值小于十为止,则所得的值为该数的 数根。例如54817的数根为7,因为5+4+8+1+7=25,25大于10则再加一次,2+5=7,7小于十,则7为54817的数根。
维基百科关于数根的定义


数根一定是0-9的数字,在维基百科种给出了计算公式



于是我们可以得到代码

#include
#include
int main()
{	
	char s[100001];
	int i,n,sum;
	while(scanf("%s",s)&&strcmp(s,"0"))
	{
		sum=0;
		n=strlen(s);
		for(i=0;i


你可能感兴趣的:(算法+ACM,acm,算法,基础题)