第2章实验总结

发布时间:2022-07-02 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了第2章实验总结脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

1.实践题目名称:二分法求单峰数组最大值

 

2.问题描述:

You are a given a unimodal array of n distinct elements, meaning that ITs entries are in increasing order up until its maximum element, after which its elements are in decreasing order. Give an algorithm to compute the maximum element that runs in O(LOG n) time.

输入格式:

An integer n in the First line, 1<= n <= 10000. N integers in the seconde line sePErated by a space, which is a unimodal array.

输出格式:

A integer which is the maximum integer in the array

输入样例:

7
1 2 3 9 8 6 5

输出样例:

93.算法描述

#include <iostream>using namespace std;

int find(int a[], int l, int r){ if(l==r) return a[r]; int mid = l + (r-l) / 2 ; if (a[mid] > a[mid + 1]) return find (a, l, mid); else return find (a, mid+1, r);}

int main(){ cout<<"请在第一行输入你的数组长度,第二行输入你的数组。数组长度不大于10000。"<<; int n; cin>> n; int a[n]; for (int i=0; i<n; i++){ cin>> a[i]; } cout<< find(a, 0, n-1); return 0; }

二分搜索:先比较数组中间两个值,如果a[mid]>a[mid+1],证明最大值在左边,于是一分为二,只考虑左边的数。以此类推,继续二分搜索直到l=r,得出结果。

循环语句:当a[mid]>a[mid+1],做上述所做的二分搜索;直到l=r。

 

4.算法@R_274_1304@和空间复杂度的分析

时间复杂度: O(log n) 

空间复杂度:O(1)

 

5.心得体会

本次实验课只完成了一道题,解题过程很艰难,首先是我理解错了题目,忽视了数组的数值呈山峰分布的题干。然后我和小伙伴参考了网上的代码,看完代码终于理解题目和解题思路,然后再一一把代码打上去。这道题也让我对二分法有了更深刻的理解。

 

6.分治法的个人体会和思考

分治法的思想和应用使算法时间复杂度成对数递减,极大地提高了算法效率。今后实际问题解决中,对于可以用分治法解决的问题,一定要秉持分治法的思想。

脚本宝典总结

以上是脚本宝典为你收集整理的第2章实验总结全部内容,希望文章能够帮你解决第2章实验总结所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。