如圖,錯誤資訊如下:
代碼如下所示,找了半天錯誤還以為自己代碼有問題,然而并不是,真是心累。。。
#include "myfun.h"
#include <iostream>
#include <algorithm>
#include <utility>
const int MAX_N = 1000000;
using namespace std;
//區間排程問題
//s:任務開始時間
//e:任務結束時間
//N:任務數量
int solve(int s[], int e[], int N)
{
pair<int, int> mCache[MAX_N];
int ans = 0, cunTime = 0;
for (int i = 0; i < N; i++)
{
mCache[i].first = e[i];
mCache[i].second = s[i];
}
sort(mCache, mCache + N);
for (int i = 0; i < N; i++)
{
if (cunTime < mCache[i].second)
{
ans++;
cunTime = mCache[i].first;
}
}
return ans;
}
int main()
{
int n, s[MAX_N], e[MAX_N];
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> s[i];
}
for (int i = 0; i < n; i++)
{
cin >> e[i];
}
int ans = solve(s, e, n);
cout << ans << endl;
system("pause");
return 0;
}
解決方法:
按照錯誤提示我們可以知道該錯誤是“棧溢出”,回頭一看可能出錯的地方也就是在定義數組的地方了,當把預定義的數組大小改成10000之後,程式居然可以運作了,由此看來是我們定義的數組太大了,是以在定義數組的時候一定要謹記千萬别太大,如果非要用容量比較大的數組的話建議可以使用new進行配置設定,然後在函數傳回時記得delete就行了。