隨機輸入一大於0而小於30的數n,請輸出第n個費氏級數
2007-07-03 15:43:36 · 2 個解答 · 發問者 阿昌 1 in 電腦與網際網路 ➔ 程式設計
#include
using namespace std;
int F(int n)
{
if( n == 0 || n == 1)
return n;
else
return F(n-1) + F(n-2);
}
void main()
{
int n;
bool bContinue = true;
do
{
cout << "輸入費氏級數的第n項 (0 < n < 30), n: ";
cin >> n;
if (0 < n && n < 30)
bContinue = false;
else
cout << "輸入錯誤, 重新輸入" << endl;
}
while (bContinue);
cout << "費氏級數第"<< n <<"項為: "<< F(n) << endl;
system("PAUSE");
}
2007-07-03 16:36:47 · answer #1 · answered by 這個世界越來越爛 4 · 0⤊ 0⤋
//Power by Visual Studio 2005
#include
#include
using namespace std;
int f(int n){
return (n>1)?f(n-1)+f(n-2):n;
}
int main(int argc, char** argv)
{
//==========START==========//
int n;
do{
cout<<"Input n: ",cin>>n;
}while(n>30||n<0);
cout<<"f("<
cout<
}
2007-07-03 17:19:43 · answer #2 · answered by Big_John-tw 7 · 0⤊ 0⤋