首页 > 试题广场 >

支付宝消费打折

[编程题]支付宝消费打折
  • 热度指数:8179 时间限制: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 个物品。
n, k = map(int, input().split())    # 获取数据
list1 = list(map(int, input().split()))
list2 = list(input())

cost_list = []
for i in range(n):    # 循环计算可打折商品打折后价格,和不打折商品组成新列表
    if list2[i] == '1':
        cost_list.append(list1[i] * 0.95)
    else:
        cost_list.append(list1[i])

cost_list.sort()    # 升序排序

count = 0
for i in range(n):    # 从最便宜的开始买,购买件数累加
    if k >= cost_list[i]:
        count += 1
        k -= cost_list[i]

print(count)
发表于 2026-02-15 16:12:36 回复(0)
import sys

while True:
    try:
        n,k=list(map(int,input().split()))
        price=list(map(int,input().split()))
        off=list(map(int,input()))
        dic=dict(zip(price,off))
        realprice=[]
        res=0
        for i in range(n):
            if off[i]==0:
                realprice.append(price[i])
            elif off[i]==1:
                realprice.append(price[i]*0.95)
        realprice=sorted(realprice)
        all_list=[]
        for i in range(n):
            allprice=res+realprice[i]
            if allprice<=k:
                res+=realprice[i]
                all_list.append(realprice[i])
            else:
                break
        print(len(all_list))
    except:
        break
发表于 2026-02-07 21:15:29 回复(0)
greedy algorithm
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)


发表于 2025-11-20 15:40:19 回复(0)