题解 | #最大报销额#

最大报销额

https://www.nowcoder.com/practice/8ec050ca75d343cfb45a305f5a7baa73




import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Scanner;

class Item{
    double price;
    String type;

    public static final HashSet<String> invoiceItem=new HashSet<>();
    static {
        invoiceItem.add("A");
        invoiceItem.add("B");
        invoiceItem.add("C");
    }

    public static Item stringToItem(String str)
    {
        String[] split = str.split(":");
        return new Item(Double.parseDouble(split[1]),split[0]);
    }

    public Item(double price, String type) {
        this.price = price;
        this.type = type;
    }

    public Item() {
    }
}
class Invoice {
    int n;
    Item[] items;
    double sumPrice=0;


    boolean isVail;

    public boolean isVail() {
        return isVail;
    }

    public static Invoice stringToInvoice(String invoice)
    {
        String[] s = invoice.split(" ");
        int n = Integer.parseInt(s[0]);
        Item[] ans = new Item[n];
        double sumPrice=0;
        boolean flag=true;
        for (int i = 1; i <= n; i++) {
            Item item = Item.stringToItem(s[i]);
            sumPrice+=item.price;
            if (item.price>600 || !Item.invoiceItem.contains(item.type))
                flag=false;
            ans[i-1]= item;
        }

        return new Invoice(n,ans,sumPrice,flag && sumPrice<=1000);
    }

    public Invoice(int n, Item[] items, double sumPrice, boolean isVail) {
        this.n = n;
        this.items = items;
        this.sumPrice = sumPrice;
        this.isVail = isVail;
    }

    public Invoice() {
    }


}
class Solution{
    double minLastPrice=Integer.MAX_VALUE;

    public double getMaxPrice(Invoice[] invoices,double maxPrice)
    {
        dfs(0,invoices,maxPrice);
        return maxPrice-minLastPrice;
    }

    void dfs(int i,Invoice[] invoices,double lastPrice)
    {
        if (i>=invoices.length)
        {
            minLastPrice= Math.min(minLastPrice,lastPrice);
            return;
        }

        dfs(i+1,invoices,lastPrice);
        if (lastPrice>=invoices[i].sumPrice && invoices[i].isVail())
        {
            dfs(i+1,invoices,lastPrice-invoices[i].sumPrice);
        }
    }

}
public class Main {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine())
        {
            double price = scanner.nextDouble();
            int n = scanner.nextInt();
            if (n==0)
                break;
            Invoice[] invoices = new Invoice[n];
            for (int i = 0; i < n;) {
                String s = scanner.nextLine();
                if (s.length()!=0)
                    invoices[i++]=Invoice.stringToInvoice(s);
            }

            System.out.printf("%.2f\n",new Solution().getMaxPrice(invoices, price));
        }
    }
}

全部评论

相关推荐

Noob1024:一笔传三代,人走笔还在
点赞 评论 收藏
分享
11-27 17:08
已编辑
牛客_产品运营部_私域运营
腾讯 普通offer 24k~26k * 15,年包在36w~39w左右。
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务