CCF考試(Java)-最大的矩形__Java

來源:互聯網
上載者:User

CCF考試(Java)-最大的矩形

試題:

試題編號: 201312-3
試題名稱: 最大的矩形
時間限制: 1.0s
記憶體限制: 256.0MB
問題描述: 問題描述   在橫軸上放了n個相鄰的矩形,每個矩形的寬度是1,而第i(1 ≤ i ≤ n)個矩形的高度是h i。這n個矩形構成了一個長條圖。例如,下圖中六個矩形的高度就分別是3, 1, 6, 5, 2, 3。



  請找出能放在給定長條圖裡面積最大的矩形,它的邊要與座標軸平行。對於上面給出的例子,最大矩形如下圖所示的陰影部分,面積是10。
輸入格式   第一行包含一個整數n,即矩形的數量(1 ≤ n ≤ 1000)。
  第二行包含n 個整數h 1, h 2, … , h n,相鄰的數之間由空格分隔。(1 ≤ h i ≤ 10000)。h i是第i個矩形的高度。 輸出格式   輸出一行,包含一個整數,即給定長條圖內的最大矩形的面積。 範例輸入 6
3 1 6 5 2 3 範例輸出 10

解答:

import java.util.*;public class Main {public static void main(String[] args) {Scanner input = new Scanner(System.in);String str[] = new String[2];int e = 0;while(e<2){str[e++] = input.nextLine();}int n = Integer.parseInt(str[0]);String[] numbers = str[1].split(" ");int[] nums = new int[numbers.length];for (int i = 0; i < nums.length; i++) {nums[i] = Integer.parseInt(numbers[i]);}int result = 0,temp = 0;boolean entry = true;for (int i = 0; i < nums.length; i++) {int l=0,r=0;for(int p=i-1;p>=0;p--){if(nums[i]<nums[p]){l++;}else{break;}}for(int q=i+1;q<nums.length;q++){if(nums[i]<nums[q]){r++;}else{break;}}if(entry){temp = result = nums[i]*(l+r+1);entry = false;}else{temp = nums[i]*(l+r+1);if(temp > result){result = temp;}}}System.out.println(result);}}

結果(不知道為什麼只得了90分):


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.