题解 | #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; }