1. 程式人生 > >JAVA-度小滿2019程式設計題

JAVA-度小滿2019程式設計題

火車站臺

火車站臺題目 注意:注意時間複雜度和空間複雜度問題 解法一:陣列法,構建data[n][2],筆者在筆試之後重新思考的解法,不過空間佔用比較大,沒有測試過大資料量的情況

    public static void getMaxValueByArr(){
        Scanner cin = new Scanner(System.in);

        //代表接下來的資料組數
        int n = cin.nextInt();
        //構建一個二維陣列儲存資料
        int[][] data = new int[n][2];
        for (int
i = 0; i < n; i++) { data[i][0] = cin.nextInt(); data[i][1] = cin.nextInt(); } //關閉資源 cin.close(); //遍歷得到的二維陣列得到最大的data[i][1] int max = 0; for (int i = 0; i < data.length; i++) { if(max<data[i][1]) max
= data[i][1]; } //通過最大值構建陣列,並進行初始化 int[] results = new int[max]; for (int i = 0; i < results.length; i++) { results[i] = 0; } //開始計算每個站點所經過的線路 for (int i = 0; i < data.length; i++) { for (int j = data[i][0]; j < data[i][1
]; j++) { results[j] = results[j]+1; } } //查詢站點和經過的線路 int count = 0; for (int i = 0; i < results.length; i++) { if(results[i]>count) count = results[i]; } System.out.println(count); }

解法二:HashMap,通過hashMap來操作,通過率36%,程式超時。

    public static void getMaxValueByMap() {
        Scanner cin = new Scanner(System.in);
        // 獲得輸入
        int n = cin.nextInt();
        // 封裝資料<站點,線路數>
        HashMap<Integer, Integer> results = new HashMap<Integer, Integer>();
        for (int i = 0; i < n; i++) {
            int startIndex = cin.nextInt();
            int endIndex = cin.nextInt();
            for (int j = startIndex; j < endIndex; j++) {
                int value = results.get(j) == null ? 1 : results.get(j) + 1;
                results.put(j, value);
            }
            System.out.println(results);
        }
        // 找出最大值
        int max = 0;
        for (int i : results.values()) {
            if (i > max)
                max = i;
        }
        System.out.println(max);
        // 關閉資源
        cin.close();
    }

買賣商品

買賣商品題目 解題思路:尋找極小值–尋找極大值–迴圈直至結束 1、基本想法是在第一個最低價買入,然後在隨後的第一個最高價賣出。 2、那麼如何求出第一個最低價呢?可以將當前價位緊隨其後的值進行比較,直到找出最低價。 3、那麼如果求出第一個最低價的隨後的第一個最高價呢?從找到的最低價後面的價位開始,尋找需要的第一個最高價。 4、得到了第一次需要買入的最低價出售的最高價之後,需要通過做差值得到利潤,然後交易次數加2(買和賣) 然後迴圈2-4步直至遍歷完所有資料。

import java.util.Scanner;
public class SecondMain {

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt();
        int[] aiList = new int[n];
        for (int i = 0; i < n; i++) {
            aiList[i] = cin.nextInt();
        }
        int prof = 0;//利潤
        int count = 0;//交易的次數
        int minPrice = 0;//
        for (int i = 0; i < n; i++) {
            // 找到第一個買入的低價 
            while (i < n - 1 && aiList[i + 1] <= aiList[i])
                i += 1;
            minPrice = aiList[i];

            //從最低價的後一個元素起,開始尋找第一個賣出的高價
            i += 1;
            while (i < n - 1 && aiList[i + 1] >= aiList[i])
                i += 1;
            /*
             * 情況1:但最低價為最後一個元素n-1,if語句不執行
             * 情況2:當最低價為倒數第二個的元素時,那麼最高價必定為最後一個.
             * */
            if (i < n) {
                // 找到的高價與找到的低價做差即可
                prof += aiList[i] - minPrice;
                // 交易的次數為偶數,買和賣,土豪不會將神祕石留在手上的
                count += 2;
            }
        }
        System.out.println(prof + " " + count);
        cin.close();
    }
}