C++ 求斐波那契数列到第42个为什么是负数啊?

C++ 求斐波那契数列的程序如下:
#include “iostream”
#include “cmath”
#include “iomanip”
using namespace std;
int main()
{
long x1, x2;
int i;
x1 = x2 = 1;
cout << “Fibonacci =” << endl;
for (i = 1; i <= 40; i++)
{
cout << setw(20) << x1 << setw(20) << x2;
if (i % 2 == 0) cout << endl;//每4个数换行输出一次
x1 = x1 + x2; //左边的x1 代表第3个数,是第1,2两个数之和
x2 = x2 + x1; //右边的x2 代表第4个数,是第3,4两个数之和
}
system(“pause”);
return 0;
}
运行的结果图:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/IgnoranceOfMe/article/details/82825048