乐高科技遥控:C语言题目!!急~~~

来源:百度文库 编辑:杭州交通信息网 时间:2024/03/29 07:29:02
题目:连续输进去一些数(遇到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
这题目好象没说是几位的数

不就这样
#include<stdio.h>
int a[999];
int i;
void main()
{
printf("输入");
for(i=0;i!=-2;i++)
{
scanf("%d",&a[i]);
if(a[i]==0)
break;
}
for(i=0;i!=-2;i++)
{
while(a[i]>9)
{
a[i]=a[i]/10+a[i]%10;
}
if(a[i]==0)
break;
}
printf("输出\n");
for(i=0;i!=-2;i++)
{
printf("%d\n",a[i]);
if(a[i]==0)
break;}
}

下面程序允许输入数为5位数以内的正整数0到99999
输入一个,处理一个。
如果你想先输完,再计算,可以修为改读入数据,存入数组,再循环处理。
例如:
long int b[100];
int N=0,j;

while ( scanf("%d",b[N]){
if (b[N] == 0) break;
N = N + 1;
}
数据共N 个,存在b[]数组内。
for (j=0;j<N;j++){
a = b[j];
这里插入类似下面的对a 的处理程序。
}
======================================
输入一个,处理一个的程序:
#include <stdio.h>
#include <stdlib.h>
main()
{
long int a;
int d[5];
int i,sum;
int l;

while ( scanf("%d",&a) ) {
if (a == 0 ) {
printf("%d\n",a); exit(0);
};

if (a < 10){
printf("%d\n",a); break;
} else
{
d[0]= a /10000;
d[1]= (a - d[0] * 10000) /1000;
d[2]= (a - d[0] * 10000 - d[1] * 1000) /100;
d[3]= (a - d[0] * 10000 - d[1] * 1000 - d[2] * 100) /10;
d[4]= a - d[0] * 10000 - d[1] * 1000 - d[2] * 100 - d[3] * 10;
sum = 0;
for (i=0;i<5;i++) sum = sum + d[i];

while (sum >= 10){
d[0]= sum/10000;
d[1]= (sum - d[0] * 10000) /1000;
d[2]= (sum - d[0] * 10000 - d[1] * 1000) /100;
d[3]= (sum - d[0] * 10000 - d[1] * 1000 - d[2] * 100) /10;
d[4]= sum - d[0] * 10000 - d[1] * 1000 - d[2] * 100 - d[3] * 10;
sum = 0;
for (i=0;i<5;i++) sum = sum + d[i];
}; // end while
printf("%d\n",sum);
}; // end if
}; // end while
exit(0);
}

这道题用long足够了

无限位都没问题

不知道是否会使用大于100以上的数