//最大子序列求解问题,并加入了一个计时器,输出的是最大子序列和,最大子序列的起始下表
#include<iostream>
#include<vector>
#include<time.h>
using namespace std;
int *maxSubSum(const vector<int> &a)
{
int* ans = new int[2];
int maxSum = 0, thisSum = 0, q = 0;
for (int j = 0;j < a.size();j++)
{
thisSum += a[j];
if (thisSum > maxSum) {
maxSum = thisSum;
}
else if (thisSum < 0) {
thisSum = 0;
q = j;
}
}
ans[0] = maxSum;
ans[1] =q;
return ans;
}
int main() {
clock_t start, finish;
double totaltime;
start = clock();
int Sum;
cout << "输入你需要输入的序列长度" << endl;
cin >> Sum;
vector<int> a2(Sum);
cout << "请输入你的序列" << endl;
for (int i = 0;i < Sum;i++) {
cin >> a2[i];
}
int *qr = maxSubSum(a2);
cout << "最大子序列和为:"<<qr[0] << endl;
cout << "子序列起始位置为:" << qr[1] << endl;
finish = clock();
totaltime = (double)(finish - start) / CLOCKS_PER_SEC;
cout << "此程序的运行时间为" << totaltime << "秒!" << endl;
system("pause"); //getchar();
return 0;
}