第一种方法:时间复杂度为:O(n * n)
#include<iostream>
using namespace std;
const int MAX = 2000;
void maxSum(int* num,int n);
int main()
{
int status = 1,j = 1, n;
int num[MAX] = {0};
while (cin >> status)
{
while (status)
{
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> num[i];
}
cout << "Case " << j << ":" << endl;
maxSum(num,n);
if (status != 1)
{
cout << endl << endl;
}
else
{
cout << endl;
}
status--;
}
}
return 0;
}
void maxSum(int* num, int n)
{
int i,j,max1= num[0],v,p,q;
for (i = 0; i < n; i++)
{
v = 0;
for (j = i; j < n; j++)
{
v += num[j];
if (v > max1)
{
max1 = v;
p = i;
q = j;
}
}
}
cout << max1 << " " << p + 1 << " " << q + 1;
}