数据结构第一节(初步认识)——三个小题

三个小题

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

给定K个整数组成的序列{ N1​​ , N​2, ..., NK​​ },“连续子列”被定义为{ N​i , N​i+1 , ..., Nj },其中 1≤i≤j≤K。“最大子列和”则被定义为所有连续子列元素的和中最大者。例如给定序列{ -2, 11, -4, 13, -5, -2 },其连续子列{ 11, -4, 13 }有最大的和20。现要求你编写程序,计算给定整数序列的最大子列和。

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

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

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

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

输入样例:

6
-2 11 -4 13 -5 -2

输出样例:

20

解决思路:动态分析,设置两个变量,一个记录历史最大子列和,另一个记录当前子列和。如果当前子列和大于历史子列和就替换掉它。如果当前子列和小于0,则将其归零重新计数。

代码:

#include<stdio.h>

int main(void) {
	int number = 0;
	scanf("%d", &number);
	int array[number];
	int temp;
	//读入数组
	for (int i = 0; i < number; i++) {
		scanf("%d", &temp);
		array[i] = temp;
	}
	int maxvalue = -1;
	int thisvalue = 0;
	for (int i = 0; i < number; i++) {
		thisvalue += array[i];
		//当前子列和大于历史子列和的替换掉它
		if (thisvalue > maxvalue) {
			maxvalue = thisvalue;
		}
		//当前子列和小于零则重新计数。
		else if (thisvalue < 0) {
			thisvalue = 0;
		}
	}
	printf("%d", maxvalue);
	return 0;
}

01-复杂度2 Maximum Subsequence Sum (25point(s))

Given a sequence of K integers{ N1​​ , N​2, ..., NK​​ }. A continuous subsequence is defined to be { N​i , N​i+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,而且还需要有一个向后走的记录可能是最大的大子列开始。等到当前大子列大于历史是,把当前大子列保存。

代码:

#include<stdio.h>
#include<stdio.h>
int main(void) {
	int number;
	int array[100000];
	scanf("%d", &number);
	int temp;
	//读入数组
	for (int i = 0; i < number; i++) {
		scanf("%d", &temp);
		array[i] = temp;
	}
	int maxvalue = -1;
	int thisvalue = 0;
	int oldbegin = 0;
	int newbegin = 0;
	int end = 0;
	int isallKNag = 1;
	for (int i = 0; i < number; i++) {
		//判断是否有大于0的数组
		if (array[i]>=0) {
			isallKNag = 0;
		}
		thisvalue += array[i];
		//如果当前子列和大于历史的,替换并且将结束下标定位到当前遍历到的下标,且保存当前最大子列和开始位置
		if (thisvalue > maxvalue) {
			maxvalue = thisvalue;
			end = i;
			oldbegin = newbegin;	
		}
		//如果当前最大子列和小于0,将其归零,并当前最大子列和开始位置设在下一位
		else if (thisvalue < 0) {
			thisvalue = 0;
			newbegin = i + 1;
		}
	}
	if (isallKNag == 0) {
		printf("%d %d %d", maxvalue,array[oldbegin],array[end]);
	}
	else {
		printf("%d %d %d", 0, array[0], array[number-1]);
	}
	return 0;
}

01-复杂度3 二分查找 (20point(s))

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

函数接口定义:

Position BinarySearch( List L, ElementType X );

其中List结构定义如下:

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

L是用户传入的一个线性表,其中ElementType元素可以通过>、==、<进行比较,并且题目保证传入的数据是递增有序的。函数BinarySearch要查找X在Data中的位置,即数组下标(注意:元素从下标1开始存储)。找到则返回下标,否则返回一个特殊的失败标记NotFound。

#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 10
#define NotFound 0
typedef int ElementType;

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

List ReadInput(); /* 裁判实现,细节不表。元素从下标1开始存储 */
Position BinarySearch( List L, ElementType X );

int main()
{
    List L;
    ElementType X;
    Position P;

    L = ReadInput();
    scanf("%d", &X);
    P = BinarySearch( L, X );
    printf("%d\n", P);

    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例1:

5
12 31 55 89 101
31

输出样例1:

2

输入样例2:

3
26 78 233
31

输出样例2:

0

解决方法:设置三个变量高中低,初始低为1,高为线性表最后一位的位置,中为高和低的中间,这是一个循环,循环的条件是(低<=高),如果线性表下标为中等于要查找的元素,返回中,如果该值大于查找值,说明查找值可能在中和低之间,令高为(中-1),同理........

代码:

//这里给出的是全代码,包括读入
#include <stdio.h>
#include <stdlib.h>

#define MAXSIZE 10
#define NotFound 0
typedef int ElementType;

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

List ReadInput(); /* 裁判实现,细节不表。元素从下标1开始存储 */
Position BinarySearch(List L, ElementType X);

int main()
{
    List L;
    ElementType X;
    Position P;

    L = ReadInput();
    scanf("%d", &X);
    P = BinarySearch(L, X);
    printf("%d\n", P);
    return 0;
}

/* 你的代码将被嵌在这里 */
List ReadInput() {
	//申请一块空间储存l,初始的最后一位是0
    List L = (List)malloc(sizeof(struct LNode));
    L->Last = 0;
    //要读多少个数?
    int num;
    scanf("%d", &num);
    //逐个读入,读入的同时增大最后一位的位置。
    for (int i = 0; i < num; i++) {
        scanf("%d", &L->Data[L->Last++]);
    }
    return L;
}

Position BinarySearch(List L, ElementType X) {
    int begin = 1, end = L->Last;
    
    while (begin <= end) {
        int mid = (begin + end) / 2;
        if (L->Data[mid]==X) {
            return (mid);
        }
        else if (L->Data[mid] > X) {
            end = mid - 1;
        }
        else {
            begin = mid + 1;
        }
    }
    return NotFound;
}

猜你喜欢

转载自www.cnblogs.com/cs-Miscellaneous/p/13405639.html