#include <iostream>
using namespace std;
int A[100010];
int main()
{
A[0] = 0;
int n = 0, m = 0;
cin >> n >> m;
int g = 0;
for (int i = 1; i <= n; ++i)
{
cin >> g;
A[i] = g;
A[i] += A[i - 1];
}
for (int j = 0; j < m; ++j)
{
int bg = 0, end = 0;
cin >> bg >> end;
int sum = A[end] - A[bg-1];
cout << sum << "\n";
}
return 0;
}
上面是段錯誤的情況...
結果后面又試了一下,加了個常量居然通過了???
#include <iostream>
using namespace std;
const int MAX = 1e7 + 10;
int A[MAX];
int main()
{
A[0] = 0;
int n = 0, m = 0;
cin >> n >> m;
int g = 0;
for (int i = 1; i <= n; ++i)
{
cin >> g;
A[i] = g;
A[i] += A[i - 1];
}
for (int j = 0; j < m; ++j)
{
int bg = 0, end = 0;
cin >> bg >> end;
cout << A[end] - A[bg - 1] << "\n";
}
return 0;
}
這是為什么啊
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/251194.html
標籤:C++ 語言
上一篇:為什么這個代碼無法讀取10.
