这天小苯来到了超市购买物品,一共有
输入包含三行。
第一行两个正整数。
第二行包含个正整数
表示每个物品的价格。
第三行一个长度为的只含有
和
的字符串,表示每个物品是否支持优惠。(如果是
代表第
个物品支持优惠,否则不支持。)
输出一行一个整数表示答案。
5 9 3 4 2 3 1 11101
4
选择买第 1,3,4,5 个物品。
#include <iostream>
#include "bits/stdc++.h"
using namespace std;
double ival[100000];
int Algorithm(int n, double k){
int sum=0;
sort(ival,ival+n);//for (int i=0;i<n;i++)cout<<ival[i]<<"\n";
int i=0;
while (ival[i]<=k && i<n){
k-=ival[i++];
sum++;
}
return sum;
}
int main() {
int n;
double k;
cin>>n>>k;
for (int i=0;i<n;i++){
cin>>ival[i];
}
string str;
cin>>str;
for (int i=0;i<n;i++){
if (str[i]=='1') ival[i]*=0.95;
}
cout<<Algorithm(n, k);
return 0;
}
// 64 位输出请用 printf("%lld") def main(): n,k = map(int,input().split()) prices = list(map(int,input().split())) s = input() cur_index = 0 for c in s: if '1' == c: prices[cur_index] = float(prices[cur_index] * 0.95) cur_index += 1 prices.sort() ans = 0 cur_index = 0 while cur_index < n and k - prices[cur_index] >= 0: k -= prices[cur_index] ans += 1 cur_index += 1 print(ans) if __name__ == '__main__': main()
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);
}
}
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String[] command = in.nextLine().split(" ");
int total = Integer.parseInt(command[0]);
double totalPrice = Double.parseDouble(command[1]);
double[] goods = Arrays.stream(in.nextLine().split(" ")).mapToDouble(Double::parseDouble).toArray();
char[] isSub = in.nextLine().toCharArray();
for (int i = 0; i < goods.length; i++) {
if (isSub[i] == '1') {
goods[i] *= 0.95;
}
}
Arrays.sort(goods);
int count = 0;
for (int i = 0; i < goods.length; i++) {
if (totalPrice >= goods[i]) {
totalPrice -= goods[i];
count++;
} else {
break;
}
}
System.out.println(count);
}
}
} n, k = map(int, input().split()) prices = list(map(int, input().split())) s = input() for i in range(n): if s[i]=='1': prices[i] = round(prices[i]*0.95, 3) prices.sort() num = 0 price = 0 for p in prices: if price+p<=k: price += p num += 1 print(num)
根据打折标志,判断商品的最终价格,然后根据最终价格排序,依次获取价格最低的商品,并与预算进行比较,获取商品数量
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);
}
} n,k=list(map(int,input().split())) n=int(n) k=int(k) l2=list(map(int,input().split())) c=input() s=0 h=0 l3=[] for i in range(n): if c[i]=="1": l3.append(l2[i]*0.95) else: l3.append(l2[i]) l4=sorted(l3) for j in l4: s+=j if k>s: h+=1 elif k<=s: h=h print(h) #90%通过率,欢迎纠正
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
static bool cmp(vector<double>& a, vector<double>& b) {
return a[1] < b[1];
}
int main() {
int n, k;
cin >> n >> k;
vector<vector<double>> goods(n, vector<double>(2, 0));
for (int i = 0; i < n; i++) {
cin >> goods[i][0];
}
string str;
cin >> str;
for (int i = 0; i < str.size(); i++) {
int isdiscount = str[i] - '0';
if (isdiscount) goods[i][1] = goods[i][0] * 0.95;
else goods[i][1] = goods[i][0];
}
sort(goods.begin(), goods.end(), cmp);
int result = 0;
double money = k;
for (int i = 0; i < n; i++) {
if (money >= goods[i][1]) {
money -= goods[i][1];
result++;
} else {
break;
}
}
cout << result;
} const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void async function () {
// Write your code here
while(line = await readline()){
let tokens = line.split(' ');
let n = parseInt(tokens[0]);
let k = parseInt(tokens[1]);
const prices = (await readline()).split(' ').map(Number);
const cutoffs = (await readline()).split('');
for (let i = 0; i < n; i++) {
if (cutoffs[i] === '1') {
prices[i] *= 0.95;
}
}
prices.sort((a, b) => a - b);
let count = 0;
for (let price of prices) {
if (price > k) {
break;
}
k -= price;
count++;
}
console.log(count);
}
}()
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String a = in.nextLine();
String b = in.nextLine();
String c = in.nextLine();
String[] bArr = b.split(" ");
//初始化商品列表
List<Good> goods = new ArrayList<>();
for (int i = 0; i < bArr.length; i++ ){
int price = Integer.parseInt(bArr[i]) ;
char discount = c.charAt(i);
double discountPrice = 0.0;
if (discount == '0'){
discountPrice = price;
} else if (discount == '1'){
discountPrice = price * 0.95;
}
goods.add(new Good(i,price,discountPrice));
}
//按照折扣后的价格排序,升序 // 按折扣后价格升序排序(核心贪心策略)
goods.sort(Comparator.comparingDouble(Good::getDiscountPrice));
String[] aArr = a.split(" ");
double money = Double.parseDouble(aArr[1]);
int max = 0;
int bLen = bArr.length;
double sum = 0.0;
final double EPS = 1e-8; // 精度误差允许范围
//进行计算,从最便宜的进行加和
for (int i = 0; i < bLen; i++){
sum += goods.get(i).getDiscountPrice();
if (sum <= money + EPS){
//没有超过余额则能购买
max++;
} else {
//超过余额则不能购买,直接退出循环
break;
}
}
System.out.println(max);
}
}
}
//商品
class Good{
int id;
int price;
//存储打折后的价格
double discountPrice;
public int getId(){
return this.id;
}
public int getPrice(){
return this.price;
}
public double getDiscountPrice(){
return this.discountPrice;
}
public Good(int id ,int price , double discountPrice){
this.id=id;
this.price=price;
this.discountPrice=discountPrice;
}
} n,k = map(int,input().split()) values = list(map(int,input().split())) zhekou = list(input()) finally_values = [] for i in range(n): if zhekou[i] == "1": finally_values.append(values[i]*0.95) else: finally_values.append(values[i]) sorted_values = sorted(finally_values) sum_value = 0 count = 0 for value in sorted_values: sum_value += value if sum_value > k: break count += 1 print(count)
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
long k = in.nextInt();
int[] a = new int[n];
for(int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
String s = in.next();
//小数乘以100,防止精度丢失
long[] cost = new long[n];
for(int i = 0; i < n; i++) {
if(s.charAt(i) == '1') {
cost[i] = a[i] * 95L;
} else {
cost[i] = a[i] * 100L;
}
}
Arrays.sort(cost);
long total = 0;
int count = 0;
long max = k * 100;
for(int i = 0; i < n; i++) {
if(total + cost[i] > max) {
break;
}
total += cost[i];
count++;
}
System.out.print(count);
}
} import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
double k = in.nextDouble();
int i = 0;
double [] a = new double[n];
while(i<n){
a[i++] = in.nextInt();
}
i = n - 1;
in.nextLine();
String s = in.nextLine();
while(i>=0){
if(s.charAt(i) == 1)
a[i] *= 0.95;
i -= 1;
}
Arrays.sort(a);
int ans = 0;
for(i =0;i<n;++i){
k -= a[i];
if(k <0)
break;
ans += 1;
}
System.out.println(ans);
}
} 这题应该要用大数乘法了,好像是用double会丢失精度,导致我的答案偏差了一些,过不了几个样例。