Advertisement
Guest User

3

a guest
Mar 30th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. int sol[100];
  4. int min(int a, int b) {
  5. if (a > b) return b;
  6. return a;
  7. }
  8. int numar(int n) {
  9. if (n == 1) return 0;
  10. if (sol[n] != -1) return sol[n];
  11. int r = 1 + numar(n - 1);
  12. if (n % 2 == 0)
  13. r = min(r, 1 + numar(n / 2));
  14. if (n % 3 == 0)
  15. r = min(r, 1 + numar(n / 3));
  16. sol[n] = r;
  17. return r;
  18. }
  19. void main() {
  20. int n;
  21. cin >> n;
  22. for (int i = 0;i <= n;i++)
  23. sol[i] = -1;
  24. cout << numar(n);
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement