Advertisement
anushervon111

Untitled

Dec 19th, 2023
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std ;
  4. int val ;
  5. bool once_used = false ;
  6. int superDigit(string n, int k) {
  7.     if (!once_used){
  8.         string adder = n ;
  9.         for (int i = 1 ; i < k; i++) {
  10.             n+=adder;  
  11.         }
  12.         once_used = true ;
  13.     }
  14.     if (n.size() == 1){return stoi(n);}
  15.     else {
  16.         int total = 0 ;
  17.         for (int i = 0 ;i < n.size(); i++)  {
  18.             total +=n[i] - '0';
  19.         }
  20.         n = to_string(total);
  21.     val =  superDigit( n, k) ;
  22.     cout << val;
  23.     }
  24.     ///cout << val ;
  25.     return val ;
  26.     //return total ;
  27. }
  28. int main() {
  29. string n ;
  30. cin >> n ;
  31. int k ;
  32. cin >> k ;
  33. cout << superDigit(n, k);
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement