一位数
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 17 Accepted Submission(s) : 9
Times New Roman | Verdana | Georgia
← →
Problem Description
对于一个正整数,递归使用如下的规则总是可以使它成为一位数,这个规则是,让这个数等于其各个数位上的数字之和,直到这个数成为一位数为止,例如,对于n=192,则n=1+9+2=12,继续使用规则,n=1+2=3,故输入192,最后输出3.
Input
输入有多组测试数据,每组包含一个正整数n(0<=n<=10^1000000).
Output
输出一个一位数,这个一位数是通过n不断地使用规则产生的。
Sample Input
192
Sample Output
3
Author
吴迎
Statistic | Submit | Back
很简单的一道题 就是用scanf接收超时了 用的getchar
#include <stdio.h>
int main()
{
char ch;
int sum=0,temp;
while(~(ch=getchar()))
{
if(ch>='0'&&ch<='9')
sum+=ch-'0';
if(sum>=10)
temp=sum%10,sum=sum/10+temp;
if(!(ch>='0'&&ch<='9'))
printf("%d\n",sum),sum=0;
}
return 0;
}