#C++初学记录(ACM试题2)
Max Sum **
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.
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).
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.
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
正确代码
#include<iostream>
#include<cstdio>
using namespace std;
int a[200];
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
int t,resmax=0,sursum=0,curleft=1,temp=0,resleft=0,resright,ressum=0;
cin>>t;
for(int i=1;i<=t;i++)
{
cin>>temp;
sursum+=temp;
if(sursum>resmax)
{
resmax=sursum;
resright=i;
resleft=curleft;
}
if(sursum<0)
{
sursum=0;
curleft=i+1;
}
}
printf("Case %d:\n%d %d %d\n",i, resmax, resleft, resright);
printf(i==t?"":"\n");
}
}
}
题意理解
题意是按照顺序进行不停的相加,假设每一次加一个数所得到的数据都另外储存在一个数组里面,则到最后进行每一步加法的大小比较,输出三组数据,第一个数据是不停相加过程中出现的最大值,第二个数据是相加过程中的起始点,注意,这里相加时若出现负数可以进行清零,然后起始点赋值成变成负值位置的下一个位置。假设中的数据实际不会用到只是为了方便解释题意,用数组进行储存每一步的相加数据太麻烦。第三个数据则是相加过程中的结束点。
对于最大值的判断:如何判断最大值是完成这个题目的首要问题,则需要一个累加的代码进行不断相加输入的数据,还需要一个判断程序,即if条件语句,进行判断是否大于“历届”最大值,最后需要一个空间储存最大值。首先定义一个空间resmax,进行初始化为零,接着对不停累加的变量sursum进行比较,与resmax进行比较,当前值大于结果值时更新resmax, resleft, resright,当前值小于0时更新curleft, cursum;。
#C++初学记录(ACM试题2)的更多相关文章
- #C++初学记录(ACM试题1)
A - Diverse Strings A string is called diverse if it contains consecutive (adjacent) letters of the ...
- #C++初学记录ACM补题(D. Candies!)前缀和运算。
D - Candies! Consider a sequence of digits of length [a1,a2,-,a]. We perform the following operati ...
- #C++初学记录(acm试题#预处理)
C - Lucky 7 in the Pocket BaoBao loves number 7 but hates number 4, so he refers to an integer as a ...
- #C++初学记录(set进阶#acm cf 190802 B. Subsegments)
B. Subsegments#set进阶 Programmer Sasha has recently begun to study data structures. His coach Stas to ...
- #C++初学记录(sort函数)
sort函数 前言:当进行贪心算法的学习时,需要用到sort函数,因为初学c++汇编语言,sort的具体用法没有深入学习,所以这里进行sort学习记录并只有基础用法并借用贪心算法题目的代码. 百度百科 ...
- 完成了C++作业,本博客现在开始全面记录acm学习历程,真正的acm之路,现在开始
以下以目前遇到题目开始记录,按发布时间排序 ACM之递推递归 ACM之数学题 拓扑排序 ACM之最短路径做题笔记与记录 STL学习笔记不(定期更新) 八皇后问题解题报告
- javaweb初学记录
原文 链接 http://blog.csdn.net/iojust/article/details/52429805 - ---热情依旧 - 环境搭建: - jdk环境配置 jdk下载: http:/ ...
- #C++初学记录(算法4)
A - Serval and Bus It is raining heavily. But this is the first day for Serval, who just became 3 ye ...
- 北大ACM试题分类+部分解题报告链接
转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 部分解题报告添加新内容,除了原有的"大致题意&q ...
随机推荐
- Elasticsearch学习之head插件安装
通过elasticseach自带的plugin命令 elasticsearch/bin/plugin -install mobz/elasticsearch-head 如下图: 2. zip包安装 ...
- jvisualvm连接远程应用终于成功,附踩大坑记录!!(一:jstatd方式)
一.问题概述 连接远程java应用除了jstatd方式,还有jmx方式.不必拘泥于一种,一种不行可以果断尝试另一种,兴许就行了. 姊妹篇在这: jvisualvm连接远程应用终于成功,附踩大坑记录!! ...
- CentOS7下Elastic Stack 5.0日志分析系统搭建
原文链接:http://www.2cto.com/net/201612/572296_3.html 在http://localhost:5601下新建索引页面输入“metricbeat-*”,之后ki ...
- nodeJS删除文件
var fs = require("fs"); var path = require("path"); deleteFolderRecursive = func ...
- vue--axios发送请求
首先安装:axios $ npm install axios $ cnpm install axios //taobao源 $ bower install axios 或者使用cdn: <scr ...
- 从底层源码浅析Mybatis的SqlSessionFactory初始化过程
目录 搭建源码环境 POM依赖 测试SQL Mybatis全局配置文件 UserMapper接口 UserMapper配置 User实体 Main方法 快速进入Debug跟踪 源码分析准备 源码分析 ...
- HTML表单的运用
没学习HTML表单之前,觉得文本框.密码框.隐藏域.单选按钮.复选框等这些在平常页面常见到的表单不起眼,挺简单,到自己用代码区实现将它们灵活串联运用起来,才发现一点都不简单.看着容易,自己操作还是出现 ...
- cp命令取消提示的方法
Linux默认cp命令带参数-i如果有重复的文件会提示覆盖 查看cp别名 在大量复制的时候这个提示不友好,在脚本写复制命令也无法使用交互式输入 解决办法 1,修改别名 vi ~/.bashrc 注释掉 ...
- HDFS 命令深入浅出
HDFS 命令深入浅出~ [root@neusoft-master ~]# hadoop dfs Usage: hadoop fs [generic options] [-appendToFile & ...
- MapReduce分组
分组:相同key的value进行分组 例子:如下输入输出,右边的第一列没有重复值,第二列取得是当第一列相同时第二例取最大值 分析:首先确定<k3,v3>,k3的选择两种方式, 方法1. ...