C++高手帮帮忙~~一道ACM题~~

来源:百度知道 编辑:UC知道 时间:2024/06/07 16:45:44
http://acm.tju.edu.cn/toj/上面的第1551题 就是讲digit root的
1551. Digital Roots
--------------------------------------------------------------------------------
Time Limit: 1.0 Seconds Memory Limit: 65536K
Total Runs: 3820 Accepted Runs: 863

--------------------------------------------------------------------------------

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. Add

参考一下我的代码吧
#include<stdio.h>
int main(){
char s[1000];
int i,n;
while(scanf("%s",s),s[0]-'0'){
n=0;
for(i=0;s[i]!='\0';i++){
n+=(s[i]-'0');
if(n>9){
if(n%9==0)
n=9;
else
n=n%9;
}
}
printf("%d\n",n);
}
return 0;
}