美團點評2018 CodeM資格賽(Java語言)

題目一

美團在吃喝玩樂等很多方面都給大家提供了便利。最近又增加了一項新業務:小象生鮮。這是新零售超市,你既可以在線下超市門店選購生鮮食品,也可以在手機App上下單,最快30分鐘就配送到家。
新店開張免不了大優惠。我們要在小象生鮮超市裏採購n個物品,每個物品價格爲ai,有一些物品可以選擇八折優惠(稱爲特價優惠)。
有m種滿減優惠方式,滿減優惠方式只有在所有物品都不選擇特價優惠時才能使用,且最多只可以選擇最多一款。
每種滿減優惠描述爲(bi,ci),即滿bi減ci(當消費>=bi時優惠ci)。
求要買齊這n個物品(必須一單買齊),至少需要多少錢(保留兩位小數)。

輸入描述:

第一行,兩個整數n,m。
接下來n行,每行一個正整數ai,以及一個0/1表示是否可以選擇特價優惠(1表示可以)。
接下來m行,每行兩個正整數bi,ci,描述一款滿減優惠。

1 <= n,m <=10
1 <= ai <= 100
1 <= ci < bi <= 1000

輸出描述:

一行一個實數,表示至少需要消耗的錢數(保留恰好兩位小數)。

示例1

輸入

2 1
6 1
10 1
12 2

輸出

12.80

示例2

輸入

2 2
6 1
10 1
5 1
16 6

輸出

10.00


Java語言解法

import java.util.Scanner;
import java.text.DecimalFormat;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int m,n;
        n = in.nextInt();
        m = in.nextInt();

        int[][] buy = new int[10][2];
        int[][] discount = new int[10][2];
        for (int i = 0; i < n; i++){
            buy[i][0] = in.nextInt();
            buy[i][1] = in.nextInt();
        }

        for (int i = 0; i < m; i++){
            discount[i][0] = in.nextInt();
            discount[i][1] = in.nextInt();
        }

        double min = 99999999.0;
        double res = 0.0;
        int resNoDis = 0;
        int all = 0;

        for (int i = 0; i < n; i++){
            all += buy[i][0];
            if (buy[i][1] == 1)
                res += buy[i][0];
            else if(buy[i][1] == 0)
                resNoDis += buy[i][0];
        }
        double temp = resNoDis + res * 0.8;
        if (temp < min)
            min = temp;

        for (int i = 0; i < m; i++){
            if (all < discount[i][0]){
                continue;
            }
            temp = all - discount[i][1];
            if (temp < min){
                min = temp;
            }
        }
        DecimalFormat df = new DecimalFormat("0.00");  
        System.out.println(df.format(min)); 
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章