唯一的坑点就是,输入的数字太长,超过 long long 的范围。所以用字符串接受,先来一遍,然后转化成数字即可。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <deque>
#include <queue>
#include <map>
using namespace std;
typedef long long ll;
const int MOD = 1e9+7;
#define Foru(i,a,b) for(i = a;i < b;i ++)
#define Ford(i,a,b) for(i = a;i > b; i --)
#define MAXN 100001
int main(){
while(true){
char ch;
ll sum = 0;
while(true){
ch = getchar();
if(ch == '\n'){
break;
}
sum += ch - '0';
}
if(sum == 0)break;
ll stem;
while(sum >= 10){
stem = sum;
sum = 0;
while(stem != 0){
sum += stem % 10;
stem /= 10;
}
}
printf("%d\n",sum);
}
return 0;
}