当前位置: 首页 > news >正文

购物消费打折

实现代码:

public static void main(String[] args) {Scanner sc = new Scanner(System.in);int n = sc.nextInt();int k = sc.nextInt();int[] prices = new int[n];for (int i = 0; i < n; i++) {prices[i] = sc.nextInt();}String discountStr = sc.next();List<Integer> discountItems = new ArrayList<>();List<Integer> noDiscountItems = new ArrayList<>();for (int i = 0; i < n; i++) {if (discountStr.charAt(i) == '1') {discountItems.add(prices[i]);} else {noDiscountItems.add(prices[i]);}}// 排序Collections.sort(discountItems);Collections.sort(noDiscountItems);int i = 0, j = 0;int count = 0;double balance = k;while (i < discountItems.size() && j < noDiscountItems.size()) {double discPrice = discountItems.get(i) * 0.95;int noDiscPrice = noDiscountItems.get(j);if (discPrice <= noDiscPrice) {if (balance >= discPrice) {balance -= discPrice;count++;i++;} else {break;}} else {if (balance >= noDiscPrice) {balance -= noDiscPrice;count++;j++;} else {break;}}}// 处理剩余优惠物品while (i < discountItems.size()) {double discPrice = discountItems.get(i) * 0.95;if (balance >= discPrice) {balance -= discPrice;count++;i++;} else {break;}}// 处理剩余非优惠物品while (j < noDiscountItems.size()) {int noDiscPrice = noDiscountItems.get(j);if (balance >= noDiscPrice) {balance -= noDiscPrice;count++;j++;} else {break;}}System.out.println(count);}

http://www.dtcms.com/a/573659.html

相关文章:

  • 深度解析Andrej Karpathy访谈:关于AI智能体、AGI、强化学习与大模型的十年远见
  • 网站403错误在线p图修改文字
  • 无锡住房建设网站社区网站建设方案书
  • 从零开始搭建 flask 博客实验(4)
  • 酒店预订数据分析及预测可视化
  • 直接IP做网站China wordpress
  • 大连建设网水电官网查询天津seo排名效果好
  • 对抗高级反爬:基于动态代理 IP 的浏览器指纹模拟与轮换策略
  • 真实场景:防止缓存穿透 —— 使用 Redisson 布隆过滤器
  • 光伏行业ERP与Oracle NetSuite:AI驱动的财务变革新范式
  • 一个本地 Git 仓库关联多个远程仓库
  • Oracle E-Business配置器运行时UI未授权访问漏洞(CVE-2025-61884)
  • iis网站架设教程软文广告300字范文
  • visual studio msvc 编译 libffi 静态库
  • 实现小程序 uniApp 输入框展示自定义表情包
  • uniapp ios android 本地离线debug
  • 基于HAL库实现GPIO输出状态控制输入状态查询及定时器PWM波和串口收发数据
  • 免费html网站哪里有广告设计培训机构
  • mysql三范式
  • RTOS多任务调度在西门子智能交通信号控制系统中的深度实践与优化
  • 【1】视觉SLAM的数学表达
  • Nginx高可用配置实战:负载均衡 + 健康检查 + 动态扩展
  • 邵阳建设银行网站是多少钱企业微信crm
  • 神经网络常见操作
  • GitCode 源启高校|北京邮电大学站:解锁开源力量,启航信息新航道
  • Hibernate之helloworld 案例
  • 建立企业网站的详细步骤爱站云网站建设heikw
  • SSM框架题库
  • 电子基础】逻辑器件全解析:从门电路到FPGA,选型与应用指南
  • 龙岩网站建设平台手机创建自己网站