浙江大学数据结构 01-复杂度2 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. 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 (≤). The second line contains Knumbers, 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 #include<iostream>
 2 using namespace std;
 3 
 4 int main()
 5 {
 6     int num,number;  
 7     int sum = 0,max = 0; //max为最大和  sum为累加值,用来遍历序列
 8     cin>>num;
 9     int index1,index2,index3,index4; //index1为第一段最大和的第一个数,index2为最大和的最后一个数 index3用来记录非第一段的最大和的第一个数 index4用来记录序列的第一个数。
10     bool flag = true;   //防止最大和序列的第一个数被改变
11     bool flag1 = true;  //用来标记每一段的开头
12     bool flag2 = true; //记录第一个数
13     while (num--) {
14         cin>>number;
15         if(flag2) {
16             flag2 = false;
17             index4 = number;
18         }
19         sum += number;
20         if(flag1 && number > 0) {
21             index3 = number;
22             flag1 = false;
23         }
24         if (sum > max) {
25             max = sum;
26             if (flag) {
27                 index1 = number;
28                 flag = false;
29             }
30             if (!flag)
31                 index1 = index3;
32             index2 = number;
33         }
34         if (sum < 0) {
35             if(!flag1)
36                 flag1 = true;
37             sum = 0;
38         }
39     }
40     if(max)
41         cout<<max<<" "<<index1<<" "<<index2<<endl;
42     else
43         cout<<max<<" "<<index4<<" "<<number<<endl;
44     return 0;    
45 } 

测试点5没过 提示负数和0

看了别人的答案用了数组来保存数据,不用数组保存确实会麻烦

 1 #include<stdio.h>
 2 int arr[10010];
 3 int main(){
 4     int n, ThisSum=0, MaxSum=-1, Minindex=0, Maxindex=0, Tempindex=0;
 5     scanf("%d", &n);
 6     for(int i=0; i<n; i++){
 7         scanf("%d", &arr[i]);
 8         ThisSum+=arr[i];
 9         if(ThisSum>MaxSum){
10             MaxSum=ThisSum;
11             Maxindex=i;
12             Minindex=Tempindex;
13         }
14         if(ThisSum<0){
15             ThisSum=0;
16             Tempindex=i+1;
17         }
18     }
19     if(MaxSum<0)printf("0 %d %d", arr[0], arr[n-1]);
20     else{printf("%d %d %d", MaxSum, arr[Minindex], arr[Maxindex]);}
21     return 0;
22 }

猜你喜欢

转载自www.cnblogs.com/Zw1999/p/10850217.html
今日推荐