PAT - 测试 01-复杂度2 Maximum Subsequence Sum (25分)
1, N2N_2N2, ..., NKN_KNK }. A continuous subsequence is defined to be { NiN_iNi, Ni+1N_{i+1}Ni+1, ..., NjN_jNj } where 1≤i≤j≤K1 \le i \le j \le K1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.
Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.
Input Specification:
Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer KKK (≤10000\le 10000≤10000). The second line contains KKK numbers, separated by a space.
Output Specification:
For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices iii and jjj (as shown by the sample case). If all the KKK numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.
Sample Input:
10
-10 1 2 3 4 -5 -23 3 7 -21
Sample Output:
),第二行输入KKK个数,以空格为分割。输出:对于每一个测试的情况下,在一个行中的最大总和的输出,与第一和最大序列的最后数字。数字必须用一个空格分开,但最后一个数后没有空格。在该最大序列不是唯一的情况下,输出一个具有最小索引的数。如果输入的所有的数都是负数,则它的最大总和被定义为0,你应该输出的第一和整个序列的最后一个数。============================第一次code:
#include <stdio.h>
#include <stdlib.h>
void MaxSubseSum(int n);
int main(void)
{
int n;
scanf("%d",&n);
MaxSubseSum(n);
;
}
void MaxSubseSum(int n)
{
,MaxSum=,start=,end=,start1=,flag=;
int a[n];
;i<n;i++)
{
scanf("%d",&a[i]);
}
;i<n;i++)
{
ThisSum += a[i];
)
flag = ;
if(ThisSum > MaxSum)
{
start = start1;
MaxSum = ThisSum;
end = i;
}
)
{
ThisSum = ;
start1=i+;
}
}
)
{
)
{
printf(],a[n-]);
}
else
{
printf("0 0 0");
}
}
else
{
printf("%d %d %d",MaxSum,a[start],a[end]);
}
}
PAT - 测试 01-复杂度2 Maximum Subsequence Sum (25分)的更多相关文章
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)
01-复杂度2 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1,N2, ..., NK }. ...
- PTA 01-复杂度2 Maximum Subsequence Sum (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/663 5-1 Maximum Subsequence Sum (25分) Given ...
- 01-复杂度2 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- 1007 Maximum Subsequence Sum (25分) 求最大连续区间和
1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- 1007 Maximum Subsequence Sum (25 分)
1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- 数据结构练习 01-复杂度2. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- 01-复杂度2. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
- PAT Advanced 1007 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- PAT 1007 Maximum Subsequence Sum (25分)
题目 Given a sequence of K integers { N1 , N2 , ..., NK }. A continuous subsequence is define ...
随机推荐
- 查询expression的小工具
今天在研究flipsolve的时候无意间写了个shelf tool,用于查询一大推节点中某些parameter的expression中是否存在我需要的关键字.就是简单的对所框选的节点进行一个循序查询参 ...
- Android 数据处理之Webapi OAuth2.0
前面通过.net Webapi搭建了数据访问及处理平台,以下介绍如何通过Android来访问Webapi的数据. Android的常用的网络访问方式是使用HttpClient和HttpURLConne ...
- Docker 容器部署 Consul 集群
Docker 容器部署 Consul 集群 一.docker安装与启动1.1安装docker[root@localhost /]# yum -y install docker-io 1.2更改配置文件 ...
- photoshop,黑白转彩色单色
方法一: 打开 色相/饱和度 面板,勾选上 着色, 然后调节 明度 滑块. 方法二: 前面方法的缺点是会将修改直接塌陷到图层,如果想不塌陷,可以使用色相蒙板: 同样勾选 着色
- C# 获取当前操作系统是32位还是64位
注:判断整型的长度的方式,只有在AnyCPU编译模式下才有用.因此更好的办法是获取真的地址总线位宽(使用WMI,windows management instruementation). .NET 2 ...
- Verilog HDL那些事_建模篇笔记(实验八:键盘解码)
1.PS2接口与协议时序图 对于PS2的接口来说,需要额外关注的是PIN5与PIN1,一个是时钟,一个是数据.PS2协议对数据的移位是“CLOCK下降沿”有效,其CLOCK的频率通常在10KHz左右. ...
- 在Ubuntu14.04系统POWER8服务器上搭建Docker Registry服务
本文描述了如何在POWER8服务器上搭建一个本地化的Docker镜像仓库,主要涉及镜像制作,Docker Registry服务启动等.希望能够对在非X86服务器上搭建Docker仓库的同学提供参考. ...
- 黄聪:C#Winform程序如何发布并自动升级(图解)
有不少朋友问到C#Winform程序怎么样配置升级,怎么样打包,怎么样发布的,在这里我解释一下打包和发布关于打包的大家可以看我的文章C# winform程序怎么打包成安装项目(图解)其实打包是打包,发 ...
- float和double在内存中的存储方式
本文转载于:http://wenku.baidu.com/link?url=ARfMiXVHCwCZJcqfA1gfeVkMOj9RkLlR9fIexbgs9gDdV8rIS48A1_xe1y6YgX ...
- Unity关于脚本前面的勾选框
今天做项目时需要在某个事件条件下禁用某个脚本,但是突然发现这个脚本前面没有勾选框,,,就像这样 网上搜了下,原来是需要在脚本中加上void Start()方法,即使这个方法里什么都没有 void St ...