-
-
Save Konard/f135af9d0b77bf85f7ee7deeb4ee87e3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
using namespace std; | |
int foo1(int n) | |
{ | |
if (n == 0) | |
{ | |
return 3; | |
} | |
if (n == 1) | |
{ | |
return 2; | |
} | |
else | |
{ | |
return foo1(n-1) * foo1(n-2) - n; | |
} | |
} | |
int foo2(int n) | |
{ | |
int nMinus2 = 3, nMinus1 = 2, result = 0; | |
if (n == 0) | |
{ | |
result = nMinus2; | |
} | |
else if (n == 1) | |
{ | |
result = nMinus1; | |
} | |
else | |
{ | |
for (int i = 2; i <= n; i++) | |
{ | |
result = nMinus2 * nMinus1 - i; | |
nMinus2 = nMinus1; | |
nMinus1 = result; | |
} | |
} | |
return result; | |
} | |
int main() | |
{ | |
int n = 0; | |
cout << "cin n: "; | |
cin >> n; | |
cout << "foo1(n): " << foo1(n) << endl; | |
cout << "foo2(n): " << foo2(n) << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment