python讲师陈越_【数据结构_浙江大学MOOC】第一讲 基本概念

本文主要是介绍python讲师陈越_【数据结构_浙江大学MOOC】第一讲 基本概念,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本系列数据结构专题为中国大学MOOC-陈越、何钦铭-数据结构-2018秋课程的学习记录。主要记录内容为编程题解答。每篇内容将会不定期更新,以补充更好的方法。

函数题

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元素可以通过>、==、

裁判测试程序样例:

#include

#include

#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

代码

编译器:C (gcc)

Position BinarySearch( List L, ElementType X ){

Position position;

Position left = 0;

Position right = L->Last;

ElementType flag = 0;

while(left <= right){

Position mid = left + (right -left)/2;

if(L->Data[mid] == X){

position = mid;

flag = 1;

break;

}

else if(L->Data[mid] > X){

right = mid-1;

}else{

left = mid+1;

}

}

if(flag == 0)

position = NotFound;

return position;

}

提交结果:

a0cbfc11d204531435543456c37ee2d9.png

编程题

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

给定K个整数组成的序列 $\{N_1,N_2, ···,N_k \}$,“连续子列”被定义为 $\{N_i,N_{i+1}, ···,N_j \}$,其中 $1≤i≤j≤K$。“最大子列和”则被定义为所有连续子列元素的和中最大者。例如给定序列 { -2, 11, -4, 13, -5, -2 },其连续子列 { 11, -4, 13 } 有最大的和20。现要求你编写程序,计算给定整数序列的最大子列和。

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

数据1:与样例等价,测试基本正确性;

数据2:$10^2$ 个随机整数;

数据3:$10^3$ 个随机整数;

数据4:$10^4$ 个随机整数;

数据5:$10^5$ 个随机整数;

输入格式:

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

输出格式:

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

输入样例:

6

-2 11 -4 13 -5 -2

输出样例:

20

代码

编译器:C (gcc)

/*

编程题-01【最大子列和问题】

中国大学MOOC-陈越、何钦铭-数据结构-2018秋

标签:复杂度

2018-09-08

*/

# include

int MaxSubseqSum(int A[], int N);

int main()

{

int numK;

int maxSum;

int k;

scanf("%d", &numK);

int a[numK];

for(k=0; k

scanf("%d", &a[k]);

maxSum = MaxSubseqSum(a, numK);

printf("%d\n", maxSum);

return 0;

}

int MaxSubseqSum (int A[], int N)

{

int thisSum, maxSum;

int i, j;

thisSum = maxSum = 0;

/* 逐个向右累加 */

for( i = 0; i < N; i++) //i 为子列左端位置

{

/* A[i]到A[j]的子列和 */

thisSum = 0;

for(j=i; j

{

/* 对于相同起点的一系列子列和,只需更新终点即可 */

/* 即对于相同的i,不同的j,只需在j-1次循环的基础上累加1项 */

/* 降低了算法复杂度 */

thisSum +=A[j];

/* 当前子列和大于最大子列和,更新 */

if(thisSum > maxSum)

maxSum = thisSum;

/* 当前子列和为负,后面再加子列和不可能变大,故抛弃 */

else if(thisSum < 0)

thisSum = 0;

}

}

return maxSum;

}

提交结果:

6a9872366aae16d433ce8a48b430c3fe.png

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

Given a sequence of $K$ integers $\{N_1,N_2, ···,N_k \}$. A continuous subsequence is defined to be $\{N_i,N_{i+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

代码

编译器:C++ (g++)

#include

#include

using namespace std;

int main(int argc, const char * argv[])

{

//input

int K;

cin>>K;

vector vec;

int value;

for (int i=0; i

{

cin>>value;

vec.push_back(value);

}

//Process

int first=0,last=0,sum=0,max=-1;

int flag_max=0;//flag represent whether max has been rewrite

int flag=0;//sum归零一次,有可能后续子数列会出现大于当前max的数列

int maybe_first=0;//可能成为最大子列和的第一个结点

for (int i=0; i

{

sum+=vec[i];

if (sum>max)//更新最大子列和

{

flag_max=1;

if (flag==0)//sum更新为0后再次出现大于之前max的最大子列和,更新最大子列和的第一个结点

{

first=maybe_first;

flag=1;

}

max=sum;

last=i;//每次出现新的最大子列和都要更新该子列的最后一个结点

}else if(sum<0)//sum出现小于0的情况,后续子列若出现更大的子列和,则该子列的第一个结点保存为maybe_first

{

maybe_first=i+1;

sum=0;

flag=0;

}

}

//Output

if (flag_max==0)

{

cout<

}else

{

cout<

}

return 0;

}

提交结果:

1a3a02d15f885471a939c8581402f291.png

编译器:Python (python3)

num = int(input())

a = input()

a = a.split(' ')

max = -1

sum = 0

temp = 0

first = 0

last = 0

for i in range(0,num):

sum = sum + int(a[i])

if sum > max:

max = sum

last = i

first = temp

elif sum < 0:

sum = 0

temp = i+1

if(max >= 0):

print(max,a[first],a[last],sep=' ')

else:

print(0,a[0],a[len(a) -1],sep=' ')

提交结果:

af28a52ca99c4bb78913604f700fb13a.png

不足之处,欢迎指正。

$$$$

这篇关于python讲师陈越_【数据结构_浙江大学MOOC】第一讲 基本概念的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/197126

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

Python中你不知道的gzip高级用法分享

《Python中你不知道的gzip高级用法分享》在当今大数据时代,数据存储和传输成本已成为每个开发者必须考虑的问题,Python内置的gzip模块提供了一种简单高效的解决方案,下面小编就来和大家详细讲... 目录前言:为什么数据压缩如此重要1. gzip 模块基础介绍2. 基本压缩与解压缩操作2.1 压缩文

Python设置Cookie永不超时的详细指南

《Python设置Cookie永不超时的详细指南》Cookie是一种存储在用户浏览器中的小型数据片段,用于记录用户的登录状态、偏好设置等信息,下面小编就来和大家详细讲讲Python如何设置Cookie... 目录一、Cookie的作用与重要性二、Cookie过期的原因三、实现Cookie永不超时的方法(一)

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Python函数作用域示例详解

《Python函数作用域示例详解》本文介绍了Python中的LEGB作用域规则,详细解析了变量查找的四个层级,通过具体代码示例,展示了各层级的变量访问规则和特性,对python函数作用域相关知识感兴趣... 目录一、LEGB 规则二、作用域实例2.1 局部作用域(Local)2.2 闭包作用域(Enclos

Python实现对阿里云OSS对象存储的操作详解

《Python实现对阿里云OSS对象存储的操作详解》这篇文章主要为大家详细介绍了Python实现对阿里云OSS对象存储的操作相关知识,包括连接,上传,下载,列举等功能,感兴趣的小伙伴可以了解下... 目录一、直接使用代码二、详细使用1. 环境准备2. 初始化配置3. bucket配置创建4. 文件上传到os

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四