#include cstdiochar str[1001];int main(){ while(scanf("%s",str)(str[0]-'0')){ int i=0; int sum=0; while(str[i]){ sum+=str[i++]-'0'; } int n=sum; while(sum9){ sum=0; while(n0){ sum+=n%10; n/=10; } n=sum; } printf("%d\n",sum); }} 【文章转自
#include <cstdio>
char str[1001];
int main(){
while(scanf("%s",str)&&(str[0]-'0')){
int i=0;
int sum=0;
while(str[i]){
sum+=str[i++]-'0';
}
int n=sum;
while(sum>9){
sum=0;
while(n>0){
sum+=n%10;
n/=10;
}
n=sum;
}
printf("%d\n",sum);
}
}