- 論壇徽章:
- 0
|
《essential c++》書中第39頁(yè)有個(gè)問題搞不明白。
fibon_elem函數(shù)的作用是根據(jù)輸入的pos位置,得出fibonncci數(shù)列在該位置上的數(shù)值。程序如下
#include <cstdlib>
#include <iostream>
using namespace std;
bool fibon_elem(int, int &);
int main(int argc, char *argv[])
{
int pos;
cout << "Please enter a position: ";
cin >> pos;
int elem;
if(fibon_elem(pos,elem))
cout << "element # " << pos << " is " << elem << endl;
else
cout << "sorry,could not calculate element # " << pos << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
bool fibon_elem(int pos, int &elem)
{
if(pos <= 0 || pos > 1024){
elem = 0;
return false;
}
elem = 1;
int n_2 = 1, n_1 = 1;
for(int ix = 3; ix <= pos; ++ix)
{
elem = n_2 + n_1;
n_2 = n_1;
n_1 = elem;
}
}
紅色代碼我認(rèn)為應(yīng)該是if(fibon_elem(pos,&elem)才對(duì)啊,但是在dev-c++下,改成if(fibon_elem(pos,&elem)反而不能通過編譯了,
請(qǐng)高手指教。謝謝 |
|