题解 | #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")

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-01 10:56
点赞 评论 收藏
分享
强大的马里奥:不太可能,我校计算机硕士就业率99%
点赞 评论 收藏
分享
05-09 13:22
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务