我需要帮助显示C++中没有数组的前100个斐波那契数
我不允许使用数组 当我的代码突然显示负数时,在某种程度上运行良好:我需要帮助显示C++中没有数组的前100个斐波那契数,c++,C++,我不允许使用数组 当我的代码突然显示负数时,在某种程度上运行良好: ... 4660046610375530309 7540113804746346429 -6246583658587674878 为什么会这样?如果没有阵列或附加库,如何修复它 #include <iostream> using namespace std; int main() { long long int n1 = 0, n2 = 1, n3, i, number; cout << &
...
4660046610375530309
7540113804746346429
-6246583658587674878
为什么会这样?如果没有阵列或附加库,如何修复它
#include <iostream>
using namespace std;
int main() {
long long int n1 = 0, n2 = 1, n3, i, number;
cout << "Enter the number of elements: ";
cin >> number;
cout << n1 << "\n" << n2 << "\n"; // printing 0 and 1
// loop starts from 2 because 0 and 1 are already printed
for (i = 2; i < number; ++i) {
n3 = n1 + n2;
cout << n3 << "\n";
n1 = n2;
n2 = n3;
}
return 0;
}
当0计为1,1计为2时,第100个斐波那契数为218922995834555169026。当0计为0,1计为1时,为354224818179261915075。这些数字超过了典型的长整型64位有符号9223372036854775807的最大值 通过存储变量的上半部分和下半部分,可以使用两个变量来表示一个整数 例如,我将在每个变量中存储12位数字 添加和打印可按如下方式进行:
#include <iostream>
#include <iomanip>
int main(void) {
const long long int half = 1000000000000LL;
long long int a_high = 83621143LL, a_low = 489848422977LL;
long long int b_high = 135301852LL, b_low = 344706746049LL;
long long int c_high, c_low;
// c = a + b
// add each digits
c_high = a_high + b_high;
c_low = a_low + b_low;
// calculate carry
c_high += c_low / half;
c_low %= half;
// print c
std::cout.fill('0');
if (c_high > 0) {
// print higher half, then lower half
std::cout << c_high << std::setw(12) << c_low;
} else {
// higher half is zero, so print lower half only
std::cout << c_low;
}
std::cout << '\n';
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
const long long int half = 1000000000000LL;
long long int n1_h = 0, n1_l = 0, n2_h = 0, n2_l = 1, n3_h, n3_l, i, number;
cout << "Enter the number of elements: ";
cin >> number;
cout << n1_l << "\n" << n2_l << "\n"; // printing 0 and 1
// loop starts from 2 because 0 and 1 are already printed
cout.fill('0');
for (i = 2; i < number; ++i) {
// n3 = n1 + n2;
n3_l = n1_l + n2_l;
n3_h = n1_h + n2_h + (n3_l / half);
n3_l %= half;
// print n3
if (n3_h > 0) {
cout << n3_h << setw(12) << n3_l;
} else {
cout << n3_l;
}
cout << "\n";
// n1 = n2;
n1_h = n2_h; n1_l = n2_l;
// n2 = n3;
n2_h = n3_h; n2_l = n3_l;
}
return 0;
}
结合Fibonacci程序,稍加改进,结果如下:
#include <iostream>
#include <iomanip>
int main(void) {
const long long int half = 1000000000000LL;
long long int a_high = 83621143LL, a_low = 489848422977LL;
long long int b_high = 135301852LL, b_low = 344706746049LL;
long long int c_high, c_low;
// c = a + b
// add each digits
c_high = a_high + b_high;
c_low = a_low + b_low;
// calculate carry
c_high += c_low / half;
c_low %= half;
// print c
std::cout.fill('0');
if (c_high > 0) {
// print higher half, then lower half
std::cout << c_high << std::setw(12) << c_low;
} else {
// higher half is zero, so print lower half only
std::cout << c_low;
}
std::cout << '\n';
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
const long long int half = 1000000000000LL;
long long int n1_h = 0, n1_l = 0, n2_h = 0, n2_l = 1, n3_h, n3_l, i, number;
cout << "Enter the number of elements: ";
cin >> number;
cout << n1_l << "\n" << n2_l << "\n"; // printing 0 and 1
// loop starts from 2 because 0 and 1 are already printed
cout.fill('0');
for (i = 2; i < number; ++i) {
// n3 = n1 + n2;
n3_l = n1_l + n2_l;
n3_h = n1_h + n2_h + (n3_l / half);
n3_l %= half;
// print n3
if (n3_h > 0) {
cout << n3_h << setw(12) << n3_l;
} else {
cout << n3_l;
}
cout << "\n";
// n1 = n2;
n1_h = n2_h; n1_l = n2_l;
// n2 = n3;
n2_h = n3_h; n2_l = n3_l;
}
return 0;
}
你的代码有什么问题?你需要什么帮助?如果您的代码有问题,请包含一个,并解释它有什么问题。注意,第100个斐波那契数不适合典型的长整型64位长。如果您运行代码,您将看到大数,它将在某个点显示负数,它将溢出,但其他情况下,它可以。你担心这个奇怪的负结果吗?第100个斐波那契数是354224848179261915075。所以你的代码仍然是incorrect@student当0为1,1为2时,第100个斐波那契数为2189229958534555169026@学生链接显示404未找到。我对顺序的定义是从您的程序继承来的,因此,如果您说它是错误的,那么您的程序不仅是因为溢出问题而错误。@student OK根据定义添加了解释。无论如何,它不会影响打印前100个斐波那契数。如果您的意思是打印到第100位,请将循环条件i