#12381: 測試時過不了 實際送出解答卻AC??


chris2012 (丁小魚)

學校 : 控江中学
編號 : 67347
來源 : [211.21.81.44]
最後登入時間 :
2019-03-05 12:55:39
d283. 大數加法 | From: [60.245.65.179] | 發表日期 : 2017-07-19 09:33

如題! 測試送出時會出現錯誤訊息! 但送出答案卻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;
}

 

 
ZeroJudge Forum