2016SDAU编程练习三1001
2016-05-15 21:29
393 查看
Problem A
Problem Description
Given a sequence a[1],a[2],a[3]......a
, 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;
}
Problem Description
Given a sequence a[1],a[2],a[3]......a
, 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;
}
相关文章推荐
- git,切换以前的版本,增加代码,然后合并的操作步骤
- springmvc之hibernate整合
- Struts2简单构建
- 用nohup重定向python输出到文件不成功的解决办法
- 为MATLAB添加深度学习工具箱
- Window64位 Win7 Python配置opencv
- [分享]ThinkPHP3.2超详细视频教程
- Thinkphp,qeephp,cakephp,zendframework,symfony 对比
- Javaworkers团队第一周项目总结
- windows下本地thinkphp环境搭建
- Java我的高效编程之环境搭建
- java多态运行时实现
- Java并发-同步器Semaphore,CountDownLatch
- python环境下SVD++推荐系统wooflix的安装
- 软件一定存在修改的余地——《大话Java性能优化》
- ASP.NET简介
- Java大牛养成记(7)——后台权限拦截器
- spring aop笔记
- C++ 重载构造函数
- cookie、session和java过滤器