# [浙江大学数据结构]PAT算法题目解析

#### 01-复杂度1 最大子列和问题（20 分）

• 数据1：与样例等价，测试基本正确性；
• 数据2：102个随机整数；
• 数据3：103个随机整数；
• 数据4：104个随机整数；
• 数据5：105个随机整数；

### 输出格式:

#include<iostream>

using namespacestd;

int main(){

int n;

cin>>n;

int a[n];

for(int i=0;i<n;i++){

cin>>a[i];

}

int sum=0;

int tsum;

for(int i=0;i<n;i++){

for(int j=i;j<n;j++){

tsum=0;

for(int k=i;k<=j;k++){

tsum+=a[k];

}

if(tsum>sum)

sum=tsum;

}

}

cout<<sum<<endl;

return 0;

}

#include<iostream>

using namespacestd;

int main(){

int n;

cin>>n;

int a[n];

for(int i=0;i<n;i++){

cin>>a[i];

}

int sum=0;

int tsum;

for(int i=0;i<n;i++){

tsum=0;

for(int j=i;j<n;j++){

tsum+=a[j];

if(tsum>sum)

sum=tsum;

}

}

cout<<sum<<endl;

return 0;

}

#include<iostream>

using namespacestd;

int main(){

int n;

cin>>n;

int a[n];

for(int i=0;i<n;i++){

cin>>a[i];

}

int sum=0;

int tsum;

for(int i=0;i<n;i++){

tsum+=a[i];

if(tsum>sum)

sum=tsum;

elseif(tsum<0)

tsum=0;

}

cout<<sum<<endl;

return 0;

}

(在线处理算法，边遍历，边处理)

#include<iostream>

using namespace std;

int main(){

int n;

cin>>n;

int a[n];

for(int i=0;i<n;i++){

cin>>a[i];

}

int b[n];

b[0]=a[0];

int sum=b[0];

for(int i=0;i<n;i++){

if(a[i]>(b[i-1]+a[i])){

b[i]=a[i];

}else{

b[i]=b[i-1]+a[i];

}

sum=(sum>b[i])?sum:b[i];

}

cout<<sum<<endl;

return 0;

}

#### 复杂度2 MaximumSubsequence Sum（25 分）

Given a sequence of K integers { N1N2,..., NK }.A continuous subsequence is defined to be { NiNi+1,..., Nj }where 1ijK.The Maximum Subsequence is the continuous subsequence which has the largest sumof its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, itsmaximum 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 twolines. 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, togetherwith the first and the last numbers of the maximum subsequence. The numbersmust be separated by one space, but there must be no extra space at the end ofa line. In case that the maximum subsequence is not unique, output the one withthe smallest indices i and j (as shown bythe sample case). If all the K numbers arenegative, then its maximum sum is defined to be 0, and you are supposed tooutput 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

#include <stdio.h>

#define  MAX100000ul

intMaxSubsequenceSum(int*p, int N, int*start, int*end)

{

int i =0,count = 0;

inttmp_start = 0;

intThisSum = 0,MaxSum = 0;

for (i= 0; i< N; i++)

{

ThisSum += *(p +i);

if(ThisSum < 0)

{

count++;

ThisSum= 0;

tmp_start = i + 1;

}

elseif(ThisSum > MaxSum)

{

MaxSum= ThisSum;

*start = *(p +tmp_start);

*end = *(p +i);

}

}

if(count == N)

{

MaxSum = 0;

*start = *(p + 0);

*end = *(p +N - 1);

}

returnMaxSum;

}

intmain(void)

{

int i;

long int k =0;

intarr[MAX] = { 0 };

intstart = 0,end = 0;

scanf("%d",&k);

for (i= 0; i< k; i++)

{

scanf("%d",&arr[i]);

}

printf("%d ",MaxSubsequenceSum(arr, k, &start, &end));

printf("%d%d",start, end);

return0;

}

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客