首页 新闻 赞助 找找看

一个求最长子序列的算法,求看看哪有问题

0
悬赏园豆:40 [待解决问题]

杭电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;
}

DrunkMan的主页 DrunkMan | 初学一级 | 园豆:166
提问于:2015-11-07 22:29
< >
分享
所有回答(2)
0

两组测试数据之间应有空行

且未考虑全部都是负数的情况

117208 | 园豆:202 (菜鸟二级) | 2016-04-08 21:36
0

话说你连个题目链接都不给,排版也没有,并且最长子序列是什么?

Aehnuwx | 园豆:263 (菜鸟二级) | 2018-07-10 13:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册