Advertisement
Josif_tepe

Untitled

Nov 11th, 2024
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. using namespace std;
  5.  
  6. int rec(int x) {
  7.     if(x == 0) {
  8.         return 0;
  9.     }
  10.     int cifra = x % 10;
  11.     return rec(x / 10) + cifra;
  12. }
  13. int main() {
  14.     cout << rec(3456) << endl;
  15.     return 0;
  16. }
  17. // rec(3456) = rec(345) + 6 = 12 + 6 = 18
  18. // rec(345)  = rec(34) + 5 = 7 + 5 = 12
  19. // rec(34)   = rec(3) + 4 = 3 + 4 = 7
  20. // rec(3)    = rec(0) + 3 = 0 + 3 = 3
  21. // rec(0) = 0
  22.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement