fork download
  1. #include <string>
  2. #include <numeric>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. int sum(string a) {
  7. if(a.size()==1) return a[0]-'0';
  8. else return sum(to_string(accumulate(a.begin(),a.end(),0,[](int acc,char l){return acc+=l-'0';})));
  9. }
  10.  
  11. int main(void) {
  12. string a;
  13. cin >> a;
  14. cout << sum(a);
  15. return 0;
  16. }
  17.  
Success #stdin #stdout 0s 3432KB
stdin
12345678901234567890
stdout
9