您的位置:首页 > 其它

HDU--1013--Digital Roots

2017-07-05 09:53 381 查看
Description
The digital rootof a positive integer is found by summing the digits of the integer. If theresulting value is a single digit then that digit is the digital root. If theresulting
value contains two or more digits, those digits are summed and theprocess 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 avalue of 6. Since 6 is a single digit, 6 is the digital root of 24. Nowconsider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 isnot a single digit, the process
must be repeated. Adding the 1 and the 2 yeilds3, a single digit and also the digital root of 39. 
Input
The input filewill contain a list of positive integers, one per line. The end of the inputwill be indicated by an integer value of zero. 
Output
For each integerin the input, output its digital root on a separate line of the output. 
Sample Input
 24
39
0
Sample Output
 6
3
 

题意:求各位上的数相加

注意,数位很长,虽然题上没说,但是通过测试可以看出来

 一:取余那个不太懂

#include <stdio.h>
int main()
{
int s,i;
char n[2000];
while(~scanf("%s",n))
{
if(n[0]=='0')
break;
s=0;
for(i=0;n[i];i++)
{
s=s+n[i]-'0';
if(s>=10)
s=s/10+s%10;
}
printf("%d\n",s);
}
}


二:自己的方法,先加一次

#include <stdio.h>
int main()
{
int s,i;
char n[2000];
int m;
while(~scanf("%s",n))
{
if(n[0]=='0')
break;
s=0;
for(i=0;n[i];i++)
{
s=s+n[i]-'0';
}
m=s;
s=0;
while(1)
{
if(m<=0&&s>=10)
{
m=s;
s=0;
}
if(m<=0&&s<10)
break;
s=s+m%10;
m=m/10;
}
printf("%d\n",s);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: