1945.cpp (416B)
1 class Solution { 2 static int sum(int n) { 3 int res = 0; 4 5 while (n > 0) { 6 res += n % 10; 7 n /= 10; 8 } 9 10 return res; 11 } 12 13 public: 14 int getLucky(const string &s, int k) const { 15 int res = 0; 16 17 for (const char c : s) 18 res += sum(c - '`'); 19 for (int i = 1; i < k; i++) 20 res = sum(res); 21 22 return res; 23 } 24 };