2243.cpp (415B)
1 class Solution { 2 public: 3 string digitSum(string s, int k) { 4 while (s.size() > k) { 5 string tmp = ""; 6 for (int i = 0; i < s.size();) { 7 int sum = 0; 8 for (int j = 0; j < k && i < s.size(); i++, j++) 9 sum += s[i] - '0'; 10 tmp += to_string(sum); 11 } 12 s = tmp; 13 } 14 return s; 15 } 16 };