第一次课:磨枪

01-复杂度1 最大子列和问题 (20分)
 

给定K个整数组成的序列{ N1,N2....NK },“连续子列”被定义为{ Ni​​, Ni+1​​, ..., Nj​​ },

其中 1。“最大子列和”则被定义为所有连续子列元素的和中最大者。例如给定序列{ -2, 11, -4, 13, -5, -2 },其连续子列{ 11, -4, 13 }有最大的和20。

现要求你编写程序,计算给定整数序列的最大子列和。

本题旨在测试各种不同的算法在各种数据情况下的表现。各组测试数据特点如下:

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

输入格式:

输入第1行给出正整数K (≤);第2行给出K个整数,其间以空格分隔。

输出格式:

在一行中输出最大子列和。如果序列中所有整数皆为负数,则输出0。

输入样例:

6
-2 11 -4 13 -5 -2
 

输出样例:20



这是用递归方法做的
 1 #include<stdio.h>
 2 
 3 int max3(int a,int b, int c);
 4 int DivideAndConqur(int left, int right,int list[]);
 5 
 6 int a[100005] = {0};
 7 int main(void)
 8 {
 9     int num; 10 scanf("%d", &num); 11 for(int i = 0; i < num; i++) 12 scanf("%d", &a[i]); 13 printf("%d",DivideAndConqur(0,num-1,a)); 14 return 0; 15 } 16 17 int max3(int a,int b, int c) 18 { 19 int max = a>b?a:b; 20 max = max>c?max:c; 21 return max; 22 } 23 24 int DivideAndConqur(int left, int right,int list[]) 25 { 26 if(left == right){ 27 if(list[left] < 0) 28 return 0; 29 else 30 return list[left]; 31  } 32 33 int son_leftsum = 0,son_max_leftsum = 0,son_rightsum = 0, son_max_rightsum = 0; 34 int leftsum = 0,max_leftsum = 0,rightsum = 0,max_rightsum = 0; 35 int mid = (left+right)/2; 36 son_max_leftsum = DivideAndConqur(left,mid,list); 37 son_max_rightsum = DivideAndConqur(mid+1,right,list); 38 39 40 for(int i = mid+1; i <= right; i++){ 41 rightsum += list[i]; 42 max_rightsum = max_rightsum > rightsum?max_rightsum:rightsum; 43  } 44 for(int i = mid-1; i >= left; i--){ 45 leftsum += list[i]; 46 max_leftsum = max_leftsum > leftsum?max_leftsum:leftsum; 47  } 48 int mid_sum = max_leftsum + max_rightsum + list[mid]; 49 return max3(mid_sum,son_max_rightsum,son_max_leftsum); 50 }
 
 
   



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 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
 
 1 #include<stdio.h>
 2 int a[100005] = {0};
 3 int main(void)
 4 {
 5     int num;
 6     int flag = 0;
 7     scanf("%d", &num);
 8     for(int i = 0; i < num; i++){
 9         scanf("%d", &a[i]);
10         if(a[i] < 0)
11             flag++;
12     }
13     //printf("%d %d\n",flag,num);
14     
15     int nowsum = 0,start = a[0],end = a[num-1],maxsum = 0;
16     int temp = 0;
17     for(int i = 0; i < num; i++){
18         if(a[i] == 0)
19             temp = i;
20         if(a[temp] == 0 && a[i] == 0)
21             continue;
22         nowsum += a[i];
23         if(nowsum < 0){
24             //printf("i = %d,nowsum = %d\n",i,nowsum);
25             nowsum = 0;
26             temp = i+1;
27             //printf("temp = %d\n",temp);
28             //printf("start = %d\n",start);
29         }
30         int pre_max = maxsum;
31         maxsum = maxsum > nowsum ? maxsum:nowsum;
32         if(maxsum != pre_max){
33             start = a[temp];
34             end = a[i];
35         }
36     }
37     if(flag == num)
38         printf("%d %d %d",0,a[0],a[num-1]);
39     else
40         printf("%d %d %d",maxsum,start>=0?start:0,end >= 0?end:0);
41 
42     return 0;
43 }
 

 做算法题不能忘了对极端情况的思考,比如全为负数,全为0等情况。

本题要求实现二分查找算法。

函数接口定义:

Position BinarySearch( List L, ElementType X );
 

其中List结构定义如下:

typedef int Position;
typedef struct LNode *List; struct LNode { ElementType Data[MAXSIZE]; Position Last; /* 保存线性表中最后一个元素的位置 */ }; 
 

L是用户传入的一个线性表,其中ElementType元素可以通过>、=、<进行比较,并且题目保证传入的数据是递增有序的。

函数BinarySearch要查找XData中的位置,即数组下标(注意:元素从下标1开始存储)。找到则返回下标,否则返回一个特殊的失败标记NotFound

裁判测试程序样例:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 
 4 #define MAXSIZE 10
 5 #define NotFound 0
 6 typedef int ElementType;
 7 
 8 typedef int Position;
 9 typedef struct LNode *List;
10 struct LNode {
11     ElementType Data[MAXSIZE];
12     Position Last; /* 保存线性表中最后一个元素的位置 */
13 }; 14 15 List ReadInput(); /* 裁判实现,细节不表。元素从下标1开始存储 */ 16 Position BinarySearch( List L, ElementType X ); 17 18 int main() 19 { 20  List L; 21  ElementType X; 22  Position P; 23 24 L = ReadInput(); 25 scanf("%d", &X); 26 P = BinarySearch( L, X ); 27 printf("%d\n", P); 28 29 return 0; 30 } 31 32 /* 你的代码将被嵌在这里 */
 

输入样例1:

5
12 31 55 89 101
31
 

输出样例1:

2
 

输入样例2:

3
26 78 233
31 
 

输出样例2:

0
 
 1 Position BinarySearch( List L, ElementType X )
 2 {
 3     int left = 1,right = L->Last;
 4     while(left <= right){
 5         int mid = (left + right) / 2;
 6         if(L->Data[mid] == X) 7 return mid; 8 else if(L->Data[mid] < X){ 9 left = mid+1; 10  } 11 else 12 right = mid - 1; 13  } 14 return NotFound; 15 }
没啥好说的
二分查找的条件是数据必须要有序的。

猜你喜欢

转载自www.cnblogs.com/csuluyao/p/12446969.html