数据结构(陈越)PAT练习题 第一周:基本概念
来源:互联网 发布:wifi登陆器源码 编辑:程序博客网 时间:2024/06/04 22:23
01-1. 最大子列和问题
给定K个整数组成的序列{ N1, N2, ..., NK },“连续子列”被定义为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j <= K。“最大子列和”则被定义为所有连续子列元素的和中最大者。例如给定序列{ -2, 11, -4, 13, -5, -2 },其连续子列{ 11, -4, 13 }有最大的和20。现要求你编写程序,计算给定整数序列的最大子列和。
输入格式:
输入第1行给出正整数 K (<= 100000);第2行给出K个整数,其间以空格分隔。
输出格式:
在一行中输出最大子列和。如果序列中所有整数皆为负数,则输出0。
输入样例:6-2 11 -4 13 -5 -2输出样例:
20
这一题非常适合使用在线算法(感觉就是为在线算法而设计的一题 =_=),基本上把姥姥课件里的示例代码拷贝过来就可以了。这一题非常简单,不做过多描述。由于当时没有保留提交的代码,这里就贴上姥姥的代码了。
int MaxSubsequenceSum( const int A[ ], int N ) { int ThisSum, MaxSum, j; ThisSum = MaxSum = 0; for ( j = 0; j < N; j++ ) { ThisSum += A[ j ]; if ( ThisSum > MaxSum ) MaxSum = ThisSum; else if ( ThisSum < 0 ) ThisSum = 0;} return MaxSum; }
01-2. Maximum Subsequence Sum
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.
Sample Input:10-10 1 2 3 4 -5 -23 3 7 -21Sample Output:
10 1 4
此题是上一题的变种版本,需要给出最大子列和的首尾元素。当输出数据全为负时,最大子列和为0,输出整个数列的第一个和最后一个元素。
需要注意的是,当数据只有0和负数时,最大子列和也为0,而这时不能输出整个数列的首元素和尾元素,因为输入数据并不全为负,而应该输出子列的首尾元素。例如:
4
-1 -2 0 -3
这时的输出结果应该为:
0 0 0
在处理时,需要两个数分别记录最大子列和的首元素和尾元素位置。最后计算出的最大子列和有两个情况:结果为正数时,则输出子列的首尾元素;结果为0时,需要进一步判断输入数据是全为负数还是有存在0元素的情况,这里我设置了一个布尔变量用于区分这两种情况。具体代码如下:
#include<iostream>using namespace std;int main(){int k;int thisSum=0, maxSum=0;int left=0, right=0;int l=0, r=0;int m;bool allNegetive = true;int* a;cin >> k;a = new int [k];for( int i=0; i<k; ++i ){cin >> a[i];}for( int i=0; i<k; ++i ){thisSum += a[i];if( a[i]>=0 ){allNegetive = false;m = i;}if( thisSum > maxSum ){maxSum = thisSum;right = i;l = left;r = right;}else if( thisSum<0 ){thisSum = 0;left = i+1;}}if( allNegetive )cout << maxSum << ' ' << a[0] << ' ' << a[k-1];else if( maxSum==0 )cout << "0 " << a[m] << ' ' << a[m];elsecout << maxSum << ' ' << a[l] << ' ' << a[r];delete [] a;return 0;}
- 数据结构(陈越)PAT练习题 第一周:基本概念
- 数据结构(陈越)PAT练习题 第四周:树(下)
- 数据结构(陈越)PAT练习题 第三周:树(上)
- 数据结构(陈越)PAT练习题 第五周:图(上)
- 数据结构(陈越)PAT练习题 第六周:图(下)
- 数据结构(陈越)PAT练习题 第七周:排序(上)
- 数据结构(陈越)PAT练习题 第八周:排序(下)
- 数据结构(陈越)PAT练习题 第二周:线性结构
- 数据结构(陈越)PAT练习题 第九周:散列查找
- 数据结构一:数据结构概论(第一周)
- Python 语言程序设计---第一周练习题
- 数据结构第一周小结
- 第一周练习题项目一练习二
- 第一周练习题项目一练习三
- 数据结构(陈越)-网易云课堂 第一周编程题
- 第一周第一节课 :什么是数据结构
- 慕课-程序设计与算法(大学先修课)-郭炜-第一周练习题
- 浙江大学数据结构第一周编程作业【Java】
- 基于AWS的媒体内容服务应用架构
- Java时间处理相对时间 之 时区相关
- Linux 命令图解5分钟系列[2] - 文件操作及目录操作进阶
- VB2010中的MediaPlayer控件在哪?
- 如何解决网站标题图标favicon.ico在搜狗浏览器不显示?
- 数据结构(陈越)PAT练习题 第一周:基本概念
- Linux加载DTS设备节点的过程(以高通8974平台为例)
- MYSQL快速备份工具xtrabackup测试(I)
- spring学习之Spring的事务配置配置篇
- 让页面加载的时候自动获得当前时间加载到DateBox中
- URL中的+,空格,/,?,%,#,&,= 及转义处理
- android基础学习之android加载大图片
- 浅析 Cordova for iOS
- cocos 触摸机制