Advertisement
jusohatam

Untitled

Dec 8th, 2021
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int recursive(int prev,int n) {
  6. if(n == 0) return 0;
  7. if(n == 1) {
  8. if (prev==0)
  9. return 2;
  10. if (prev==1)
  11. return 1;
  12. }
  13. if(prev==0)
  14. return recursive(0,n-1)+recursive(1,n-1);
  15. if(prev==1)
  16. return recursive(0,n-1);
  17. }
  18.  
  19. int count_strings_recursive(int n) {
  20. return recursive(0,n);
  21.  
  22. }
  23.  
  24. int count_strings_dynamic(int n) {
  25. int a[n], b[n];
  26. a[0]=b[0]=1;
  27. for (int i = 1; i < n; i++) {
  28. a[i] = a[i-1] + b[i-1];
  29. b[i] = a[i-1];
  30. }
  31. return a[n-1] + b[n-1];
  32. }
  33.  
  34. void menu() {
  35. cout << "Menu:\n";
  36. cout << "1) Recursive solution of task\n";
  37. cout << "2) Dynamic solution os task\n";
  38. cout << "0) Exit\n";
  39. cout << "~\n";
  40. }
  41.  
  42. int main() {
  43. int n, op = -1;
  44. while (op != 0) {
  45. menu();
  46. cin >> op;
  47. cout << "Enter N:\n";
  48. cin >> n;
  49. if (op == 1) {
  50. cout << "Answer: " << count_strings_recursive(n) << "\n";
  51. } else if (op == 2) {
  52. cout << "Answer: " << count_strings_dynamic(n) << "\n";
  53. }
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement