Advertisement
Josif_tepe

Untitled

Nov 16th, 2021
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4. int rec(string s, int i) {
  5.     if(i == -1) {
  6.         return 0;
  7.     }
  8.     int cifra = 0;
  9.     if(isdigit(s[i])) {
  10.         cifra = (s[i] - '0');
  11.         return rec(s, i - 1)  * 10 +  cifra;
  12.     }
  13.     else {
  14.         return rec(s, i - 1);
  15.     }
  16.    
  17. }
  18. int main()
  19. {
  20.     string s;
  21.     cin >> s;
  22.     cout << rec(s, s.size() - 1) << endl;
  23.     return 0;
  24. }
  25. /*
  26.  rec(123abc45, 7) = rec(123abc45, 6) * 10 + 5 = 1234 * 10 + 5 = 12345
  27.  rec(123abc45, 6) = rec(123abc45, 5) * 10 + 4 = 123 * 10 + 4 = 1234
  28.  rec(123abc45, 5) = rec(123abc45, 4) = 123
  29.  rec(123abc45, 4) = rec(123abc45, 3) = 123
  30.  rec(123abc45, 3) = rec(123abc45, 2) = 123
  31.  rec(123abc45, 2) = rec(123abc45, 1) * 10 + 3 = 12 * 10 + 3 = 123
  32.  rec(123abc45, 1) = rec(123abc45, 0) * 10 + 2 = 1 * 10 + 2 = 12
  33.  rec(123abc45, 0) = rec(123abc45, -1) * 10 + 1 = 0 * 10 + 1 = 1
  34.  rec(123abc45, -1) = 0
  35.  
  36.  **/
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement