难度:2
考虑到数字一开始可能很长,所以先用字符串处理
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
int solve(int n) {
int a;
while(n >= 10) {
a = n;
n = 0;
while(a) {
n += a % 10;
a /= 10;
}
}
return n;
}
char ch[100000];
int main() {
int n;
while(~scanf("%s" , ch)) {
int len = strlen(ch);
if(len == 1 && ch[0] == '0') break;
n = 0;
for(int i=0;i<len;i++) n += ch[i] - '0';
int ans = solve(n);
printf("%d\n" , ans);
}
return 0;
}