首页 > 试题广场 >

支付宝消费打折

[编程题]支付宝消费打折
  • 热度指数:5547 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
众所周知,在一些消费支付的场合中,往往有“支付宝九五折”的优惠。
这天小苯来到了超市购买物品,一共有 n 种物品,每种物品只能购买一个,但有的物品支持优惠活动,有的并不支持。恰好本超市的结账是有“支付宝九五折”优惠的,小苯的支付宝余额还剩 k 元,他想知道他仅使用支付宝进行支付的话,最多能买几件物品?

输入描述:
输入包含三行。
第一行两个正整数 n, k\ (1 \leq n \leq 10^5), \ (1 \leq k \leq 10^9)
第二行包含 n 个正整数 a_i (1 \leq a_i \leq 10^4) 表示每个物品的价格。
第三行一个长度为 n 的只含有 01 的字符串,表示每个物品是否支持优惠。(如果是 1 代表第 i 个物品支持优惠,否则不支持。)


输出描述:
输出一行一个整数表示答案。
示例1

输入

5 9
3 4 2 3 1
11101

输出

4

说明

选择买第 1,3,4,5 个物品。

根据打折标志,判断商品的最终价格,然后根据最终价格排序,依次获取价格最低的商品,并与预算进行比较,获取商品数量

注意货币计算,原则上不允许用double和float,只能用整数
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);
    }
}


发表于 2025-11-19 02:41:01 回复(0)
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int n = in.nextInt();
        int k = in.nextInt();
        Double[] arr = new Double[n];
        for(int i = 0; i < n; i++){
            arr[i] = in.nextDouble();
        }
        in.nextLine();
        String str = in.nextLine();
        for(int i = 0; i < n; i++){
            arr[i] = str.charAt(i) == '1' ? arr[i] * 0.95 : arr[i];
        }
        Arrays.sort(arr);
        int num = 0;
        double sum = 0;
        while(num < n){
            if(sum + arr[num] > k){
                break;
            }else{
                sum += arr[num];
                num++;
            }
        }
        System.out.println(num);
    }
}
发表于 2025-09-04 23:58:20 回复(0)
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);
    }
}

发表于 2025-08-30 20:19:37 回复(0)