这天小苯来到了超市购买物品,一共有
输入包含三行。
第一行两个正整数。
第二行包含个正整数
表示每个物品的价格。
第三行一个长度为的只含有
和
的字符串,表示每个物品是否支持优惠。(如果是
代表第
个物品支持优惠,否则不支持。)
输出一行一个整数表示答案。
5 9 3 4 2 3 1 11101
4
选择买第 1,3,4,5 个物品。
根据打折标志,判断商品的最终价格,然后根据最终价格排序,依次获取价格最低的商品,并与预算进行比较,获取商品数量
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int count = in.nextInt();
long totalAmount = in.nextLong() * 100L;
int[] goodsPrice = new int[count];
List<Long> list = new ArrayList<>();
for (int i = 0; i < count; i++) {
goodsPrice[i] = in.nextInt();
}
char[] alipayFlagArray = in.next().toCharArray();
for (int i = 0; i < count; i++) {
if ('1' == alipayFlagArray[i]) {
list.add(goodsPrice[i] * 95L);
} else {
list.add(goodsPrice[i] * 100L);
}
}
list.sort(null);
long local = 0L;
int result = 0;
for (Long goods : list) {
local += goods;
if (local <= totalAmount) {
result++;
} else {
break;
}
}
System.out.println(result);
}
} import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
double k = in.nextDouble(); // 支付宝余额
in.nextLine(); // 消耗换行符
// 读取物品价格
int[] price = new int[n];
for (int i = 0; i < n; i++) {
price[i] = in.nextInt();
}
in.nextLine(); // 消耗换行符
// 读取折扣信息
String discountStr = in.nextLine();
// 计算最终价格
double[] finalPrice = new double[n];
for (int i = 0; i < n; i++) {
if (discountStr.charAt(i) == '1') {
// 支持优惠,九五折
finalPrice[i] = price[i] * 0.95;
} else {
// 不支持优惠,原价
finalPrice[i] = price[i];
}
}
// 排序,优先购买便宜的物品
Arrays.sort(finalPrice);
// 计算最多能购买的数量
int count = 0;
double remaining = k;
for (double p : finalPrice) {
if (remaining >= p) {
remaining -= p;
count++;
} else {
break;
}
}
System.out.println(count);
}
}