数据结构(陈越)PAT练习题 第一周:基本概念

来源:互联网 发布:wifi登陆器源码 编辑:程序博客网 时间:2024/06/04 22:23

01-1. 最大子列和问题

时间限制
10000 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard

给定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

时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

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 -21
Sample 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;}


0 0
原创粉丝点击