当前位置: 代码迷 >> 数码设备 >> 杭电 1013 digital roots 错哪了
  详细解决方案

杭电 1013 digital roots 错哪了

热度:539   发布时间:2016-04-29 02:26:56.0
杭电 1013 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.


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.
我的两种代码自己检测的时候都是对的,提交后就是错的,我错在哪里了?是输出格式错误还是哪里?我觉得我没写错呀

我的两种代码:

代码1:

/* ************************ *给定一个正整数N,求N!的位数  *********************** */ #include <stdio.h>#include <string.h>#define MAXSIZE 1000int main(int argc,char *argv[]){	char str[MAXSIZE];	int i=0;		while(1)	{		int sum=0;		scanf("%s",str);		if(strcmp(str,"0")==0)		{			break;		}		for(i=0;i<MAXSIZE-1&&str[i]!='\0';i++)		{						sum+=str[i]-'0';			if(sum>9)			{				sum=sum/10+sum%10;			} 		}		printf("%d\n",sum); 	}	return 0;}
代码2:

/* ************************ *给定一个正整数N,求N!的位数  *********************** */ #include <stdio.h>#include <math.h>int do_sum(int);int main(int argc,char *argv[]){	int M;	int mol,sum=0;	scanf("%d",&M);	while(M!=0)	{		while(M>=10)		{			M=do_sum(M);		} 		printf("%d\n",M);				scanf("%d",&M);    } 	return 0;}int do_sum(int M){	int sum=0,mol;	while(M!=0)		{			mol=M%10;			sum=sum+mol;			M=M/10;		}	return sum;}