Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3.  
  4. static unsigned long long *arr1;
  5. static unsigned long long *arr2;
  6. static unsigned long long *arr3;
  7.  
  8. unsigned long long stairs(int n, char step)
  9. {
  10. if (n > 0)
  11. {
  12. if (step == 1)
  13. {
  14. if (arr1[n]) return arr1[n];
  15. return arr1[n] = stairs(n - 2, 2) + stairs(n - 3, 3);
  16. }
  17. if (step == 2)
  18. {
  19. if (arr2[n]) return arr2[n];
  20. return arr2[n] = stairs(n - 1, 1) + stairs(n - 3, 3);
  21. }
  22. if (step == 3)
  23. {
  24. if (arr3[n]) return arr3[n];
  25. return arr3[n] = stairs(n - 2, 2) + stairs(n - 1, 1);
  26. }
  27. return stairs(n - 2, 2) + stairs(n - 1, 1) + stairs(n - 3, 3);
  28. }
  29. if (n == 0) return 1;
  30. return 0;
  31. }
  32.  
  33. int main()
  34. {
  35. int n;
  36. //std::cin >> n;
  37.  
  38. for (int i = 1; i < 1000; ++i)
  39. {
  40. arr1 = new unsigned long long[i + 1]{ 1 };
  41. arr2 = new unsigned long long[i + 1]{ 1 };
  42. arr3 = new unsigned long long[i + 1]{ 1 };
  43.  
  44. clock_t begin = clock();
  45. std::cout << i << " - " << stairs(i, 0) << ' ';
  46. clock_t end = clock();
  47. int delta_ms = (end - begin) * 1000 / CLOCKS_PER_SEC;
  48. std::cout << " time: "<< delta_ms << " ms\n";
  49.  
  50. delete[] arr1;
  51. delete[] arr2;
  52. delete[] arr3;
  53. }
  54. system("pause");
  55. return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement