手机电话图片:C语言程序题!!高手进

来源:百度文库 编辑:高校问答 时间:2024/04/28 05:57:20
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 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.

Example

Input

24
39
0
Output

6
3
不是啊,题目要求这样:
如输进去:
24
39
0
输出:
6
3
不是每次输一个,输出一个 !

那个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)
{
while(n >= 10)
n=ss(n);
printf("%d\n",n);
}

}

这个就是可以取很多数一直到0的阿

#include<stdio.h>
int lenth(int n)
{
int sum=0;
do
{
sum=sum+n%10;
n=n/10;
}
while(n!=0);
return sum;
}

int main()
{
int n;
scanf("%d",&n);
while(n>=10)
n=lenth(n);
printf("%d",n);
return 0;
}

我的程序,测试成功
输入456,输出6
输入78,输出6
输入14,输出5