酒店平台网站建设,贵港建设局网站查询,招聘58同城找工作,中山seo外包1
题目
这天小苯来到了超市购买物品#xff0c;一共有 几种物品#xff0c;每种物品只能购买一个#xff0c;但有的物品支持优惠活动#xff0c;有的并不支持#xff0c;恰好本超市的结账是有“支付宝九五折”优惠的#xff0c;小苯的支付宝余额还剩 人元#xff0c;他…1
题目
这天小苯来到了超市购买物品一共有 几种物品每种物品只能购买一个但有的物品支持优惠活动有的并不支持恰好本超市的结账是有“支付宝九五折”优惠的小苯的支付宝余额还剩 人元他想知道他仅使用支付宝进行支付的话最多能买几件物品?
输入描述
输入包含三行。 一行两个正整
第二行包含 n个正整数 ai(1 a: 104)表示每个物品的价格。
第三行一个长度为 n的只含有0和1的字符串表示每个物品是否支持优惠。(如果是1代表第之个物品支持优惠否则不支持。) 代码
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;public class BuyMore {public static void main(String[] args) {Scanner scanner new Scanner(System.in);System.out.println(请输入物品的数量n: );int n scanner.nextInt(); // 读取物品数量System.out.println(请输入支付宝余额k: );int k scanner.nextInt(); // 读取支付宝余额// 存放每个物品的价格ArrayListInteger prices new ArrayList();System.out.println(请输入每件物品的价格);for (int i 0; i n; i) {prices.add(scanner.nextInt()); // 读取每个物品的价格}scanner.nextLine(); // 读取行尾的换行符System.out.println(请输入每件物品是否参与优惠);// 控制台输入字符串 代表每件物品是否参与优惠String supports scanner.nextLine(); // 读取每个物品是否支持优惠ArrayListInteger discountPrices new ArrayList(); // 存储支持优惠的物品价格ArrayListInteger nonDiscountPrices new ArrayList(); // 存储不支持优惠的物品价格// 遍历support字符串 分别存储支持和不支持优惠的物品价格for (int i 0; i n; i) {if (supports.charAt(i) 1) {discountPrices.add(prices.get(i));} else {nonDiscountPrices.add(prices.get(i));}}Collections.sort(discountPrices);Collections.sort(nonDiscountPrices);// 初始化可以购买的物品数int itemsBought 0;// 首先考虑支持优惠的物品for (int price : discountPrices) {int discountedPrice (int)Math.ceil(price * 0.95); // 应用九五折优惠并向上取整if (k discountedPrice) {k - discountedPrice; // 从余额中扣除金额itemsBought;} else {break;}}// 然后考虑不支持优惠的物品for (int price : nonDiscountPrices) {if (k price) {k - price;itemsBought;} else {break;}}System.out.println(itemsBought); // 输出最多能买的物品数量scanner.close();}
}