- 论坛徽章:
- 0
|
《essential c++》书中第39页有个问题搞不明白。
fibon_elem函数的作用是根据输入的pos位置,得出fibonncci数列在该位置上的数值。程序如下
#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;
}
}
红色代码我认为应该是if(fibon_elem(pos,&elem)才对啊,但是在dev-c++下,改成if(fibon_elem(pos,&elem)反而不能通过编译了,
请高手指教。谢谢 |
|