C语言程序题!!高手进

来源:百度知道 编辑:UC知道 时间:2024/05/27 11:27:43
C语言题目!!急~~~
题目:连续输进去一些数(遇到0退去),如输进去24,把它的个十位分开,加起来为6;
如输进去十39,它的个十位相加为12,然后继续把它的个十位分开,直到加起来为一个个位数为止
如:输进去
24
39
0
输出:
6
3

这道题目的英文是: Background

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 pos

那个ss函数也有问题,不能输入比较大的数.比如输入78,它会显示15.下面是我改过的,测试成功.如要把最大允许输入增大,只要改N后面的数字即可.
#define N 50
int ss(int num)
{
int sum=0,k=0;
while(num>=10)
{
sum=num;
k=0;
while(sum!=0)
{
k+=sum%10;
sum/=10;
}
num=k;
}
return num;
}

int main()
{
int a[N]={0},i=0;
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
if(a[i]==0)break;
a[i]=ss(a[i]);
}
for(i=0;a[i]!=0;i++)
printf("%d\n",a[i]);
}

do while 1
for (i=1;i<=lenth(n);i++)
{sum=sum+n%10;
n=n/10;}
if sum<10 break;else n=sum;
loop

大该算法就是这个了,语法不太记得了。自己补充下吧~
哎~现在作业都可以网上代做了!
没注意看~试下动态数组~

ACM...

#include "stdio.h"
int ss(int num)
{
int sum=0;
while(num) {sum+=num%10 ; num/=10 ; }
return sum;
}

int main()
{
int n;

while(scanf("%d",&n)==1 && n)