Advertisement
Josif_tepe

Untitled

Mar 27th, 2022
760
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <algorithm>
  6. #include <set>
  7. #include <cstring>
  8. #include <cmath>
  9. #include <map>
  10. #include <fstream>
  11. using namespace std;
  12.  
  13. int f(int x) {
  14.     if(x == 0) {
  15.         return 0;
  16.     }
  17.     return f(x / 10) + (x % 10);
  18. }
  19. int main() {
  20.  
  21.     cout << f(1234) << endl;
  22.    
  23.    return 0;
  24. }
  25. /*
  26.  f(1234) --> f(123) + 4 = 6 + 4 = 10
  27.  f(123) --> f(12) + 3 = 3 + 3 = 6
  28.  f(12) --> f(1) + 2 = 1 + 2 = 3
  29.  f(1) --> f(0) + 1 = 0 + 1 = 1
  30.  f(0) = 0
  31.  
  32.  */
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement