数据结构(陈越 、何钦铭)--第一周编程作业

**

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

**
给定K个整数组成的序列{ N​1, N2, …, NK },“连续子列”被定义为{ N​i, N​i+1,…,N​j},其中 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

#include <stdio.h>
#define N 100000

int FindMaxSum(int a[], int K);

int main()
{
    int K, a[N], maxsum;
    int i;
    /*save the data in the a[]*/
    //printf("Please press a number K:\n");
    scanf("%d", &K);
    //printf("Press the numbers:\n");
    for (i = 0; i < K; i++)
    {
        scanf("%d", &a[i]);
    }
    maxsum = FindMaxSum(a, K);
    printf("%d", maxsum);
    return 0;
}

int FindMaxSum(int a[], int K)
{
    int maxsum, thissum, i;
    maxsum = thissum = 0;
    for (i = 0; i < K; i++)
    {
        thissum += a[i];
        if (maxsum < thissum)
            maxsum = thissum;
        if (thissum < 0)
            thissum = 0;
    }
    return maxsum;
}
//C++ language
#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::vector;

int FindMax(vector<int> a, int mount);

int main() {
	int mount{ 0 }, item{ 0 };
	int maxthum{ 0 };

	//read the mount of the numbers
	cin >> mount;

	if (mount > 100000) { return 0; }

	//read the data, build the a[mount]
	vector<int> a(mount);
	for (int i = 0; i < mount; i++) {
		cin >> item;
		a[i] = item;
	}

	//get the result
	maxthum = FindMax(a, mount);

	//output the result
	cout << maxthum << endl;

	return 0;
}

int FindMax(vector<int> a, int mount) {
	int maxthum{ 0 }, thum{ 0 };
	for (auto i : a) {
		thum += i;
		maxthum = (thum > maxthum) ? thum : maxthum;
		thum = (thum >= 0) ? thum : 0;
	}
	return maxthum;
}
// 复习的时候写的
#include <iostream>

using namespace std;

int main() {
	int N{ 0 }, maxSum{ 0 }, Sum{ 0 }, item{ 0 };
	cin >> N;
	if (N > 0) {
		for (int i = 0; i < N; i++) {
			cin >> item;
			Sum += item;
			if (Sum < 0)	Sum = 0;
			if (maxSum < Sum)	maxSum = Sum;
		}
	}
	cout << maxSum;
	return 0;
}

**

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

**
Given a sequence of K integers { N1, N​2, …, NK}. A continuous subsequence is defined to be { N​i, Ni+1, …, N​j} 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

/*
这里比之前的最大子序列之和所多出来的地方就是需要提取出最大子序列的头和尾
这里用到了一个特别巧妙的参数,其实际功能是记录可能会出现的新的子序列的和的头,我管它叫 'magic'
这么说吧,如果 head 参数是记录目前存在的最大子序列的头,那么 magic 参数就是要记录下一个头,含有这个头的子序列一旦比之前的子序列还要打,那么此时就将 magic 的值赋予 head
至于说 tail 的值,就随着每次 max 的变动,一旦变动,此时加上去的数,就是 tail
*/
#include <stdio.h>
#define N 10000

void FindMaxSeq(int a[], int K);

int main()
{
    int K, a[N], i;
    /*save the data in a[K]*/
    scanf("%d", &K);
    for (i = 0; i < K; i++)
    {
        scanf("%d", &a[i]);
    }
    FindMaxSeq(a, K);
    return 0;
}

void FindMaxSeq(int a[], int K)
{
    int maxsum, thissum, i;
    int firstp, lastp, magic = 0;
    maxsum = -1;
    thissum =0;
    for (i = 0; i < K; i++)
    {
        thissum += a[i];
        if (maxsum < thissum)
            {
                maxsum = thissum;
                lastp = i;
                firstp = magic;
            }
        if (thissum < 0)
            {
                thissum = 0;
                magic = i + 1;
            }
    }

    if (maxsum < 0)
        printf("0 %d %d", a[0], a[magic - 1]);
    else
        printf("%d %d %d", maxsum, a[firstp], a[lastp]);
    return;
}
//c++ language
#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::endl;
using std::vector;

vector<int> FindMax(vector<int> a, int mount);

int main() {
	int mount{ 0 }, item{ 0 };
	int maxthum{ 0 };

	//read the mount of the numbers
	cin >> mount;

	if (mount > 100000) { return 0; }

	//read the data, build the a[mount]
	vector<int> a(mount);
	for (int i = 0; i < mount; i++) {
		cin >> item;
		a[i] = item;
	}

	//get the result
	//vector<int> result{maxthum, head, tail};
	vector<int> result{ 0, 0, 0 };
	result = FindMax(a, mount);

	//output the result
	cout << result[0] << " " 
		 << result[1] << " "
		 << result[2];

	return 0;
}

vector<int> FindMax(vector<int> a, int mount) {
	vector<int> result{ 0, 0, 0 };
	int maxthum{ -1 }, thum{ 0 };
	int head{ 0 }, tail{ 0 };
	for (int i = 0; i < a.size(); i++) {
		thum += a[i];
		tail = i;
		if (thum > maxthum) {
			maxthum = thum;
			result[1] = head;
			result[2] = tail;
		}
		if (thum < 0) {
			thum = 0;
			head = i + 1;
		}
	}
	result[0] = maxthum;

	if (maxthum < 0) {
		result[0] = 0; result[1] = a[0]; result[2] = a[tail];
	}
	return result;
}

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

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

函数接口定义:

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

Position BinarySearch( List L, ElementType X )
{
    int left = 1; int right = L->Last; 
    int mid = left + (right - left)/2;
    
    while(left <= right)
    {
        if(L->Data[mid] < X)
        {
            left = mid + 1;
            mid = left + (right - left)/2;
        }
        
        if(L->Data[mid] > X)
        {
            right = mid - 1;
            mid = left + (right - left)/2;
        }
        
        if(L->Data[mid] == X)
        {
            return mid;
        }
    }
    return NotFound;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

比巧克力巧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值