1007. Maximum Subsequence Sum
来源:互联网 发布:js获取list第一个元素 编辑:程序博客网 时间:2024/05/29 15:58
暴力解决
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 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.
#include <stdio.h>#include <stdlib.h>#include <string.h>#define N 10001int main (){ int k; scanf("%d",&k); int num[N]; int flag=0; int i,sum; for( i=0;i<k;i++) { scanf("%d",num+i); if(num[i]>=0) flag=1; } if( flag==0) { printf("%d %d %d\n",0,num[0],num[k-1]); return 0; } int max=-1,start,end; for (int i=0;i<k;i++) { sum=0; for (int j=i;j<k;j++) { sum+=num[j]; if ( sum>max) { max=sum; start=num[i]; end=num[j]; } } } printf("%d %d %d\n",max,start,end); system("pause"); return 0; }
- 1007. Maximum Subsequence Sum
- 1007.Maximum Subsequence Sum
- 1007. Maximum Subsequence Sum
- 1007. Maximum Subsequence Sum
- 1007. Maximum Subsequence Sum
- 1007. Maximum Subsequence Sum
- 1007. Maximum Subsequence Sum
- 1007. Maximum Subsequence Sum
- 1007. Maximum Subsequence Sum
- 1007. Maximum Subsequence Sum
- 1007. Maximum Subsequence Sum
- 1007. Maximum Subsequence Sum (25)
- 1007. Maximum Subsequence Sum (25)
- (PAT)1007. Maximum Subsequence Sum
- (PAT)1007. Maximum Subsequence Sum
- 1007. Maximum Subsequence Sum (25)
- 1007. Maximum Subsequence Sum (25)
- pat 1007. Maximum Subsequence Sum
- 年终总结-致敬2014
- PCI9054 突发模式数据传输 (burst mode data transfer )
- String创建对象理解
- 从零开始学习jquery (一)
- velocity逃逸符号
- 1007. Maximum Subsequence Sum
- eclipse 快捷键记录
- 时序设计中的异步时钟寄存器同步
- Maven常用命令
- 面试10大算法汇总
- MySQL非安装版的配置
- Spring JDBC详解
- sql常用语句(w3c)
- 【C#】 1. 创建Array数据类型