如題! 測試送出時會出現錯誤訊息! 但送出答案卻AC 求幫助>"<
錯誤訊息:
系統呼叫了 abort 函式!
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted (core dumped)
原始碼:
#include <iostream>
#define N 20001 // 項數
#define W 600 // 位數
using namespace std;
int main() {
int i, j;
int **ptr = NULL;
ptr = new int*[N];
for (i = 0; i < N; i++) {
ptr[i] = new int[W];
}
for (i = 0; i < N; i++) {
for (j = 0; j < W; j++) {
ptr[i][j] = 0;
}
}
ptr[0][0] = 0; //可省略
ptr[1][0] = 1;
//___________________________________
int tem = 0;
for (i = 2; i < N; i++) {
for (j = 0; j < W; j++) {
ptr[i][j] = ptr[i - 1][j] + ptr[i - 2][j] + tem;
if (ptr[i][j]>=10000) {
tem = ptr[i][j] / 10000;
ptr[i][j] = ptr[i][j] % 10000;
}
else
tem = 0;
}
}
int n;
while (cin >> n) {
for (i = n, j = W - 1; j >= 0; j--)
if (ptr[i][j] != 0)
break;
for ( ; j >= 0; j--) {
cout << ptr[i][j];
}
cout << endl;
}
delete[] ptr;
return 0;
}