题解 | #Digital Roots#

Digital Roots

https://www.nowcoder.com/practice/cef727d0af33479c9fb4a9c120702414

#include <iostream>
#include <vector>
using namespace std;

int sum_num(vector<int>& a) {
    int sum = 0;
    for (int i = 0; i < a.size(); i++) sum += a[i];
    return sum;
}
int main() {
    string str;
    while (cin >> str) {
        vector<int> a;
        for (int i = str.size() - 1; i >= 0; i--) a.push_back(str[i] - '0');
        int sum = sum_num(a);
        if (sum < 10) cout << sum << endl;
        else {
            while (1) {
                a.clear();
                while (sum) {
                    a.push_back(sum % 10);
                    sum /= 10;
                }
                sum =sum_num(a);
                if(sum<10){
                    cout<<sum<<endl;
                    break;
                }
            }
        }
    }
    return 0;
}

全部评论

相关推荐

喜欢走神的孤勇者练习时长两年半:爱华,信华,等华,黑华
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务