[PAT] 1007 Maximum Subsequence Sum (25 分)Java

1007 Maximum Subsequence Sum (25 分)

Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+1​​, ..., N​j​​ } where 1≤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 K (≤10000). The second line contains K 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 i and j (as shown by the sample case). If all the K 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:

10 1 4


 1 import java.io.BufferedReader;
 2 import java.io.IOException;
 3 import java.io.InputStreamReader;
 4 
 5 /**
 6  * @Auther: Xingzheng Wang
 7  * @Date: 2019/2/18 15:56
 8  * @Description: pattest
 9  * @Version: 1.0
10  */
11 public class PAT1007 {
12     public static void main(String[] args) throws IOException {
13         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
14         reader.readLine();
15         String[] split = reader.readLine().split(" ");
16         int final_max_sum = Integer.MIN_VALUE;
17         int sub_sum = 0, start_index = 0, final_start = 0, final_end = split.length - 1;
18         for (int i = 0; i < split.length; i++) {
19             int value = Integer.valueOf(split[i]);
20             sub_sum = sub_sum + value;
21             if (sub_sum >= 0) {
22                 if (sub_sum > final_max_sum) {
23                     final_max_sum = sub_sum;
24                     if (start_index != final_start) {
25                         final_start = start_index;
26                     }
27                     final_end = i;
28                 }
29             } else {
30                 sub_sum = 0;
31                 start_index = i + 1;
32             }
33         }
34         if (final_max_sum < 0) {
35             final_max_sum = 0;
36         }
37         System.out.print(final_max_sum + " ");
38         System.out.print(Integer.valueOf(split[final_start]) + " ");
39         System.out.println(Integer.valueOf(split[final_end]));
40     }
41 }

 

转载于:https://www.cnblogs.com/PureJava/p/10497952.html

VS2022如何兼容老项目?.NET Framework 4.0/4.5安装避坑指南 本文提供了在Visual Studio 2022中兼容和维护.NET Framework 4.0/4.5等老旧项目的完整解决方案。针对VS2022无法直接识别旧框架的核心问题,文章详细剖析了原因,并重点推荐通过官方NuGet包安全获取引用程序集的方法,给出了从下载、解压到部署至标准目录的详细步骤。同时,文章还深入探讨了项目文件格式过旧、NuGet包还原失败等进阶问题的排查与解决策略,旨在帮助开发者高效、稳定地在现代开发环境中维护历史代码库。 阅读详情

相关推荐

微信小程序 | 小程序开发

事实上,目前小程序在我们生活中已经随处可见(特别是这次疫情的推动,不管是什么岗位、什么年龄阶段的人,到哪都需要打开健康码)WePY是由腾讯开源的,一款让小程序支持组件化开发的框架,通过预编译的手段让开发者可以选择自己喜欢的开发风格去开发小程序。Mpvue是一个使用Vue开发小程序的前端框架,也是 支持 微信小程序、百度智能小程序,头条小程序 和 支付宝小程序;微信小程序、支付宝小程序、淘宝小程序、抖音小程序、头条小程序、QQ小程序、美团小程序等。,其他平台的小程序往往是平台本身的一些公司或者顺手开发的。

CoderHing的博客~ 1万+

1007 Maximum Subsequence Sum (25 分) Java实现

1007 Maximum Subsequence Sum (25 分) Java实现 Given a sequence of K integers { N1, N2, …, NK }. A continuous subsequence is defined to be { Ni, Ni+1, …, Nj } where 1≤i≤j≤K. The Maximum Subsequence is the...

uu22222的博客 538

【一张图系列】别再混淆了!5分钟搞懂ERP、MES、PLC的职责边界与数据流》

《ERP、MES、PLC系统边界与协作解析》一文清晰阐述了制造业三大核心系统的角色分工与数据流转。文章将ERP比作企业"大脑"负责战略规划,MES作为车间"指挥官"负责执行调度,PLC则是设备"士兵"执行具体操作。通过一张架构图展示了从计划层到控制层的完整信息流:蓝色箭头表示指令下行(ERP→MES→PLC),橙色箭头代表数据上行反馈(PLC→MES→ERP)。文章强调理清系统边界对数字化转型、部门协作和项目实施的关键作用,并预告下期将深入解析ME

「东哥说MES」 专注于MES制造执行系统,为你拆解核心架构、功能模块及与ERP/PLC的集成逻辑。提供从入门到精通的系列干货、精选行业报告与实用工具,构建你的数字化知识体系,关注我,用实战经验照亮你的数字化之路。 970

1007 Maximum Subsequence Sum (25 分) java

解题思路: 最大子列和问题,题目不需要得到所有等长的子列和,相等的取前面的即可,简化了题目。 最大子列和的最好时间复杂度是O(N),所以应该尽量想着一遍遍历求出答案,题目要求输出最大子列和,子列和final_sum,开始&&结束,对应的值,想到这比较符合数组的存储结构,遍历下标,start对应 一个,end对应一个,再用start和end访问数组就得到结果了。除此之外还需要两个中间变量,一个是遍历过程中的sub_sum,一个index用来更新start的值,当遍历数组过程中sub_s.

LangWeiXian_的博客 244

字节跳动高频111道核心前端面试题解析,web开发增强

但是本人觉得看书也很必要,可以节省很多时间,常见的javascript的书,例如:javascript的高级程序设计,是每位前端工程师必不可少的一本书,边看边用,了解js的一些基本知识,基本上很全面了,如果有时间可以读一些,js性能相关的书籍,以及设计者模式,在实践中都会用的到。内容包括html,css,JavaScript,ES6,计算机网络,浏览器,工程化,模块化,Node.js,框架,数据结构,性能优化,项目等等。自定义指令(v-check、v-focus)的方法有哪些?v-model是什么?

2401_84094620的博客 1200

SVDD和OCSVM的比较

OCSVM(one class support vector machine)即单类支持向量机,最先提出的文献为:Estimating the support of a high-dimensional distribution.该模型 将数据样本通过核函数映射到高维特征空间,使其具有更良好的聚集性,在特征空间中求解一个最优超平面实现目标数据与坐标原点的最大分离,如图1: 坐标原...

qq_33880788的博客 1万+

1007. Maximum Subsequence Sum (25)(java版)

Given a sequence of K integers { N1, N2, …, NK }. A continuous subsequence is defined to be { Ni, Ni+1, …, Nj } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has t

啦啦啦的博客 391

别再手动跑定时任务了!Spring Boot项目5分钟集成xxl-job保姆级教程(含Docker部署调度中心)

本文提供Spring Boot项目快速集成xxl-job的保姆级教程,包括Docker部署调度中心的详细步骤。通过实战案例展示如何构建企业级分布式任务调度系统,解决传统定时任务的资源竞争、单点故障等问题,提升开发效率与系统可靠性。

weixin_30315905的博客 291

[PAT]1007. Maximum Subsequence Sum (25)@Java

1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a sequence of K integers { N

zjkC050818的博客 577

在ZYNQMP上点亮800x480 LCD屏:从framebuffer到DRM框架的完整驱动移植实战

本文详细介绍了在ZYNQMP平台上点亮800x480 LCD屏的完整驱动移植实战,从framebuffer到DRM框架的技术实现。内容涵盖硬件配置、设备树设置、显示时序参数调试以及DRM驱动核心实现,帮助开发者快速掌握PetaLinux系统中的LCD驱动开发技巧。

weixin_33701294的博客 427

解决Todesk连接卡在100%问题:remote display server is not supported (e.g. Wayland)

摘要:解决Ubuntu 22.04远程桌面连接问题(AnyDesk报错"remote display server not supported"及向日葵/Todesk无法显示)。方法:1)安装lightdm显示管理器;2)启用lightdm并禁用gdm;3)编辑/etc/gdm3/custom.conf文件,添加WaylandEnable=false;4)重启系统。注:后续可通过命令切换回gdm显示管理器。该方法适用于解决Wayland显示协议导致的远程桌面连接兼容性问题。

猿小白的博客 1573

PAT 甲级 1007 Maximum Subsequence Sum (25 分)(Java)

文章目录PAT 甲级 1007 Maximum Subsequence Sum (25 分)(Java)题目题意解读解题思路解法解法一解法二 PAT 甲级 1007 Maximum Subsequence Sum (25 分)(Java) 题目 题目链接 题意解读 这题是经典的动态规划题:最大连续子序列和,这题多出来的就是如何存储这个最大子序列和的起始点和终点; 解题思路 这题可以首先写出最基础的最大连续子序列和,然后在此基础上思考如何通过一定的数据结构保存最大连续子序列和的起点和终点; 需要注意的是:

盼你到天明的博客 308

3、灵巧手的设计与构造

本文详细探讨了灵巧手的设计与构造,包括多指结构、关节和连杆设计、传感器集成以及分层控制架构等内容。同时,通过工业装配、医疗手术辅助和日常生活辅助等实际应用案例,展示了灵巧手的灵活性和适应性,并分析了当前设计面临的挑战及未来发展方向。

brandy的博客 490

pat甲级1007 Maximum Subsequence Sum (动态规划)

1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+1​​, ..., N​j​​ } where 1≤i≤j≤K. The Maximum Su...

whisperlzw的博客 286

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

1007 Maximum Subsequence Sum (25 分) java 题解

Given a sequence ofKintegers {N1​,N2​, ...,NK​}. A continuous subsequence is defined to be {Ni​,Ni+1​, ...,Nj​} where1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given seq...

深街酒徒的博客 240

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

[Java] 1007. Maximum Subsequence Sum (25)-PAT甲级

1007. Maximum Subsequence Sum (25)Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 &lt;= i &lt;= j &lt;= K. The Maximum Subse...

柳婼 の blog 719

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

PAT 1007 Maximum Subsequence Sum(最长子段和)

PAT 1007 Maximum Subsequence Sum(最长子段和)

Dacc123的博客 1641

1007 Maximum Subsequence Sum

题目来源:PAT (Advanced Level) Practice Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+1​​, ..., N​j​​ } where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the lar

weixin_51032998的博客 173

PAT甲级1007

1007 Maximum Subsequence Sum (25 分) 最大字段和加记录开始结束位置 1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<algorithm> 5 #include<cmath> 6 ...

weixin_30644369的博客 303

PAT甲级1007 Maximum Subsequence Sum 测试点5不通过解决

1007 Maximum Subsequence Sum Given a sequence of K integers { N​1​​, N​2​​, …, N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+1​​, …, N​j​​ } where 1≤i≤j≤K. The Maximum Subsequence is the continuous subsequence which has the largest sum of

weixin_43608553的博客 1022

PAT甲级练习1007. Maximum Subsequence Sum (25)

1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a sequence of K integers { N

showerGG的博客 347

1007 Maximum Subsequence Sum(25 分)(PAT甲级)

Problem Description: Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+1​​, ..., N​j​​ } where 1≤i≤j≤K. The Maximum Subsequence is th...

第一楼主的博客 826

PAT 1007. Maximum Subsequence Sum (25)

1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a sequence of K integers { N

神州之光的专栏 732

PAT-Java-1007. Maximum Subsequence Sum (25)

1007. Maximum Subsequence Sum (25) 题目阐述 Given a sequence of K integers { N1, N2, …, NK }. A continuous subsequence is defined to be { Ni, Ni+1, …, Nj } where 1 Now you are supposed to find

rebornyp的博客 577

pat 1007 Maximum Subsequence Sum --java解题

题意: 给定一个K整数序列{n1, n2,…n k !连续子序列定义为{N i, N i+1,…最大子序列是指其元素和最大的连续子序列。 例如,给定序列{- 2,11,- 4,13,-5,-2},其最大子序列为{11,- 4,13},其最大和为20。现在你应该找到最大的和,以及最大子序列的第一个和最后一个数字。 输出要求: 对于每个测试用例,在一行中输出最大的和,以及最大子序列的第一个和最后一个数字。 这些数字必须用一个空格隔开,但是在一行的末尾不能有额外的空格。如果最大子序列不是唯一的,则输出索引i和j

不知而求知_的博客 208

PAT 甲级 1007 Maximum Subsequence Sum【水题,注意一个超时样例】

求最大连续子序列,一开始我是直接三重循环算的。。但是最后一个样例超时。 其实不用三重循环,二重循环就可以算出来了,三重循环相当于多计算和比较了许多次。 for(int i=0;i<n;i++){ for(int j=i;j<n;j++){ sum += a[j]; if(a[i]<0) con...

之虎者也的博客 237
上一篇: [PAT]1006 Sign In and Sign Out (25 分)Java
下一篇: [PAT] 1008 Elevator (20 分)Java
Simma1101
博客等级 码龄12年 0粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值