牛客139306865号 level
获赞
1
粉丝
1
关注
4
看过 TA
5
广西大学
2014
Java
IP属地:广东
暂未填写个人简介
私信
关注
2022-04-25 14:37
广西大学 Java
小明需要购买6斤苹果,1斤梨子,两种水果 苹果2元一斤,梨子5元一斤 超时搞活动,有两种套餐 套餐1:4斤苹果 1斤梨子 6元 套餐2:5斤苹果 1斤梨子 12元 需要最少花多少钱买齐水果(种类+斤数)?  解释: 第一行:需要购买水果的水果种类与斤数,使用空格隔开 第二行:每种水果单价(元/斤) 第三行开始为套餐,最后一个数字为套餐价格,前面数字为每种水果的斤数 约束: 1-水果种类不超过10 2-水果单价不超过10 3-套餐种类不超过10 4-购买的水果斤数必需为指定水量,即使套餐更优惠  输入 6 2 2 5 4 1 6 5 1 12 输出 10 因为:购买套餐1后,再单买2斤苹果
牛客27401890...:public class ApplePear { private static Combo c1,c2; private static int aPrice,pPrice; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int apple = 0, pear = 0; String[] fs = br.readLine().split(" "); apple = Integer.parseInt(fs[0]); pear = Integer.parseInt(fs[1]); if (apple == 0 &;&; pear == 0) { System.out.println(0); return; } String[] prices = br.readLine().split(" "); aPrice = Integer.parseInt(prices[0]); pPrice = Integer.parseInt(prices[1]); String[] com1 = br.readLine().split(" "); c1 = new Combo( Integer.parseInt(com1[0]), Integer.parseInt(com1[1]), Integer.parseInt(com1[2]) ); String[] com2 = br.readLine().split(" "); c2 = new Combo( Integer.parseInt(com2[0]), Integer.parseInt(com2[1]), Integer.parseInt(com2[2]) ); int res = dfs(apple, pear); System.out.println(res); } private static int dfs(int apple, int pear) { if (apple == 0 &;&; pear == 0) { return 0; } if (apple == 0 &;&; pear != 0) { return dfs(0, pear-1) + pPrice; } else if (apple != 0 &;&; pear == 0) { return dfs(apple-1, 0) + aPrice; } else { if (c1.isOk(apple, pear)) { int valTao = dfs(apple - c1.apple, pear - c1.pear) + c1.prices; return valTao; } else if (c2.isOk(apple, pear)) { int valTao = dfs(apple - c2.apple, pear - c2.pear) + c2.prices; return valTao; } else { return dfs(apple-1, pear-1) + aPrice + pPrice; } } } private static class Combo { public final int apple; public final int pear; public final int prices; public Combo (int apple, int pear, int prices) { this.apple = apple; this.pear = pear; this.prices = prices; } public boolean isOk(int apple, int pear) { if (apple >= this.apple &;&; pear >= this.pear) { return true; } return false; } } }
投递荣耀等公司10个岗位
0 点赞 评论 收藏
分享

创作者周榜

更多
关注他的用户也关注了:
牛客网
牛客网在线编程
牛客网题解
牛客企业服务