杭电1003题,我测试了很多数据都没问题,一直报wa,求大神看看
#include <iostream>
#include <string>
using namespace std;
int main()
{
int num, num2;
cin >> num;
for (int i = 1; i <= num; ++i) {
int n;
cin >> n;
int* i1 = new int[n];
for (int j = 0; j < n; ++j) {
cin >> i1[j];
}
int startIndex, endIndex;
startIndex = endIndex = 0;
int max, temp;
max = temp = i1[0];
for (int j = 1; j < n; ++j) { // 寻找最大子串的末尾位置
temp += i1[j];
if (max < temp) {
max = temp;
endIndex = j;
}
}
temp = max;
for (int j = startIndex; j < endIndex; ++j) { // 寻找最大子串的开始位置
temp -= i1[j];
if (max < temp) {
max = temp;
startIndex = j + 1;
}
} // end for
cout << "Case " << i << ":" << endl;
cout << max << " " << startIndex + 1 << " " << endIndex + 1;
if (i != num)
cout << endl;
}
return 0;
}
两组测试数据之间应有空行
且未考虑全部都是负数的情况
话说你连个题目链接都不给,排版也没有,并且最长子序列是什么?