Java教程

算法第二章上机实践报告

本文主要是介绍算法第二章上机实践报告,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
7-1 maximum number in a unimodal array (40 分)  

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
  结尾无空行

输出样例:

9
 
 

 

   这道题用了分而治之策略里面的二分法,极大的缩小了运行时间,时间复杂度为O(logn),空间复杂度为O(logn)。

  同时分而治之之策略的框架为:

         分解原问题,解决子问题,合并问题解。就是将问题细化,找到最“单纯”的子问题就比较好解题。

  另外我们要注意一些特殊情况,比如单调递增序列或者单调递减序列等等,这时问题解往往为边缘值。

 

这篇关于算法第二章上机实践报告的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!