2016SDAU程式設計練習三1001
Problem A
Problem Description
Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.<br>
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).<br>
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence.
If there are more than one result, output the first one. Output a blank line between two cases.<br>
Sample Input
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
Sample Output
Case 1:
14 1 4
Case 2:
7 1 6
題意:求子序列最大和
思路:基礎動態規劃
感想:最近總是搞系統,這個也沒時間做
AC程式碼:
#include<iostream>
#include<string.h>
#include<set>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<numeric>
#include<math.h>
#include<string.h>
#include<sstream>
#include<stdio.h>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<map>
#include<queue>
#include<iomanip>
#include<cstdio>
using namespace std;
int main()
{
//freopen("r.txt","r",stdin);
int n,m,sum,maxx,j,k;
int a[100005];
cin>>n;
int ans=1,step;
while(n--)
{
cin>>m;
for(int i=0;i<m;i++)
{
scanf("%d",&a[i]);
}
sum=0;
maxx=-1001;
step=1;
for(int i=0;i<m;i++)
{
sum+=a[i];
if(sum>maxx)
{
j=step;
k=i+1;
maxx=sum;
}
if(sum<0)
{
sum=0;
step=i+2;
}
}
cout<<"Case "<<ans++<<":"<<endl;
cout<<maxx<<" "<<j<<" "<<k<<endl;
if(n>0) cout<<endl;
}
return 0;
}