帮忙找下有什么问题。。郁闷死!
一个求数字字根的题:原题: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.
输入提示:
24
39
0
对应的输出提示:
6
3
我的程序有两个,,但是在杭电上面都是出现Wrong Answer..
1:
#include"stdio.h"
int main()
{
int n;
while(scanf("%d",&n)!=EOF&&n!=0)
{
if(n%9==0)
{
printf("%d\n",9);
}
else
{
printf("%d\n",n%9);
}
}
}
----------------------------------------
#include"stdio.h"
int main()
{
int n,a,c,i;
int b[100];
while(scanf("%d",&n)!=EOF&&n!=0)
{
st:
c=n;
for(i=1;;i++)
{
c=c/10;
if(c==0)
break;
}
a=i;
for(i=0;i<a;i++)
{
b[i]=n%10;
n=n/10;
}
for(i=0;i<a;i++)
{
c=c+b[i];
}
if(c<10)
printf("%d\n",c);
else if(c==10)
printf("%d\n",1);
else if(c>10)
{
n=c;
goto st;
}
}
}