题解 | #Digital Roots#
Digital Roots
https://www.nowcoder.com/practice/cef727d0af33479c9fb4a9c120702414
对于深度未知的子类问题,应当使用递归
#include <iostream>
using namespace std;
string root(string s){
if(s.size()==1) return s;
int temp=0;
for(int i=0;i<s.size();i++){
temp+=s[i]-'0';
}
return root(to_string(temp));
}
int main() {
string a;
while (cin >> a ) { // 注意 while 处理多个 case
// cout << a + b << endl;
cout<<root(a)<<endl;
}
}
// 64 位输出请用 printf("%lld")