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

全部评论

相关推荐

点赞 评论 收藏
分享
让资本家给我当牛做马:26的秋招还没开始啊?你找的是实习?实习的话你马上就研三了为什么还要实习?
点赞 评论 收藏
分享
06-16 15:04
黑龙江大学 Java
零OFFER战士:另一个版本查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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