华为OD机试 - 模拟商场优惠打折(Python/JS/C/C++ 2024 E卷 200分)

news2024/9/30 20:57:00

在这里插入图片描述

华为OD机试 2024E卷题库疯狂收录中,刷题点这里

专栏导读

本专栏收录于《华为OD机试真题(Python/JS/C/C++)》。

刷的越多,抽中的概率越大,私信哪吒,备注华为OD,加入华为OD刷题交流群,每一题都有详细的答题思路、详细的代码注释、3个测试用例、为什么这道题采用XX算法、XX算法的适用场景,发现新题目,随时更新,全天CSDN在线答疑。

一、题目描述

某网上商城举办优惠活动,发布了满减、打折、无门槛3种优惠券,分别为:

1.每满100元优惠10元,无使用数限制,如100199元可以使用1张减10元,200299可使用2张减20元,以此类推;
2.92折券,1次限使用1张,如100元,则优惠后为92元;
3.无门槛5元优惠券,无使用数限制,直接减5元。

每次最多使用2种优惠券,2种优惠可以叠加(优惠叠加时以优惠后的价格计算),以购物200元为例,可以先用92折券优惠到184元,再用1张满减券优惠10元,最终价格是174元,也可以用满减券2张优惠20元为180元,再使用92折券优惠到165(165.6向下取整)元,不同使用顺序的优惠价格不同,以最优惠价格为准。在一次购物中,同一类型优惠券使用多张时必须一次性使用,不能分多次拆开穿插使用(不允许先使用1张满减券,再用打折券,再使用一张满减券)。

请设计实现一种解决方法,帮助购物者以最少的优惠券获得最优的优惠价格。优惠后价格越低越好,同等优惠价格,使用的优惠券越少越好,可以允许某次购物不使用优惠券。

优惠活动每人只能参加一次,每个人的优惠券种类和数量是一样的。

二、输入描述

第一行:每个人拥有的优惠券数量(数量取值范围为[0, 10]),按满减、打折、无门槛的顺序输入。
第二行:表示购物的人数n(1 <= n <= 10000)。
最后n行:每一行表示某个人优惠前的购物总价格(价格取值范围(0, 1000],都为整数)。

约定:输入都是符合题目设定的要求的。

三、输出描述

每行输出每个人每次购物优惠后的最低价格以及使用的优惠券总数量,每行的输出顺序和输入的顺序保持一致。

补充说明

1.优惠券数量都为整数,取值范围为[0, 10]。
2.购物人数为整数,取值范围为[1, 10000]。
3.优惠券的购物总价为整数,取值范围为(0, 1000]。
4.优惠后价格如果是小数,则向下取整,输出都为整数。

四、测试用例

1、输入

3 2 5
3
100
200
400

2、输出

65 6
155 7
338 4

3、说明

输入3个人,输出3行结果,同输入的顺序,对应每个人的优惠结果,如下:
第一行:先使用1张满减券优惠到90元,再使用5张无门槛券优惠25元,最终价格是65元,总共使用6张优惠券
第二行:先使用2张满减券优惠到180元,再使用5张无门槛券优惠25元,最终价格是155元,总共使用7张优惠券
第三行:先使用1张92折券优惠到368元,再使用3张满减券优惠30元,最终价格是338元,总共使用4张优惠券

五、解题思路

  1. 读取输入:
    • 首先读取每种优惠券的数量,包括满减券、打折券和无门槛5元优惠券。
    • 然后读取购物人数以及每个人的购物总价格。
  2. 枚举优惠券组合:
    • 对于每个人的购物总价格,枚举所有可能的优惠券组合,包括:
    • 只使用一种优惠券(满减券、打折券、或5元优惠券)。
    • 使用两种不同类型的优惠券,并考虑不同的应用顺序。
  3. 计算优惠后价格:
    • 对于每种优惠券组合,按照不同的应用顺序计算最终的优惠后价格和使用的优惠券数量。
    • 在计算过程中,确保不超过每种优惠券的使用限制,并且优惠后的价格不能低于0。
  4. 选择最优结果:
    • 对于每个人,选择能够使最终价格最低的优惠券组合。
    • 如果存在多个组合达到相同的最低价格,选择使用优惠券数量最少的组合。
    • 允许不使用任何优惠券,以确保在某些情况下价格不会被错误地降低。
  5. 输出结果:
    • 输出每个人的最终优惠后价格以及使用的优惠券总数量。

六、Python算法源码

# 导入必要的模块
import sys
import math

def calculate_final_price(original_price, full_reduction, discounts, coupons):
    """
    计算单个购物的最终价格和使用的优惠券数量
    :param original_price: 原始价格
    :param full_reduction: 满减券数量
    :param discounts: 打折券数量
    :param coupons: 无门槛5元优惠券数量
    :return: 最终价格和使用的优惠券总数量
    """
    # 初始化最优价格为原价,使用优惠券数量为0
    min_price = original_price
    min_coupons = 0

    # 定义优惠券类型
    # 0: 满减券
    # 1: 打折券
    # 2: 无门槛5元优惠券

    # 枚举所有可能的优惠券组合,包括使用0、1或2种类型的优惠券
    # 首先考虑不使用任何优惠券
    options = []
    options.append((original_price, 0))

    # 单一类型优惠券
    # 使用满减券
    if full_reduction > 0:
        price_after_full = original_price
        coupons_used = 0
        # 每满100元使用1张满减券,减10元
        num_full = min(full_reduction, price_after_full // 100)
        price_after_full -= num_full * 10
        coupons_used += num_full
        if price_after_full < 0:
            price_after_full = 0
        options.append((price_after_full, coupons_used))
    
    # 使用打折券
    if discounts > 0:
        price_after_discount = math.floor(original_price * 0.92)
        coupons_used = 1
        options.append((price_after_discount, coupons_used))
    
    # 使用5元优惠券
    if coupons > 0:
        price_after_coupon = original_price
        # 使用尽可能多的5元优惠券,不超过可用数量和不超过价格
        num_coupon = min(coupons, price_after_coupon // 5)
        price_after_coupon -= num_coupon * 5
        coupons_used = num_coupon
        options.append((price_after_coupon, coupons_used))
    
    # 两种类型的优惠券组合
    # 满减券 + 打折券
    if full_reduction > 0 and discounts > 0:
        # 先使用满减券,再使用打折券
        price1 = original_price
        coupons_used1 = 0
        num_full1 = min(full_reduction, price1 // 100)
        price1 -= num_full1 * 10
        coupons_used1 += num_full1
        price1 = math.floor(price1 * 0.92)
        coupons_used1 += 1
        if price1 < 0:
            price1 = 0
        options.append((price1, coupons_used1))
        
        # 先使用打折券,再使用满减券
        price2 = original_price
        coupons_used2 = 0
        price2 = math.floor(price2 * 0.92)
        coupons_used2 += 1
        num_full2 = min(full_reduction, price2 // 100)
        price2 -= num_full2 * 10
        coupons_used2 += num_full2
        if price2 < 0:
            price2 = 0
        options.append((price2, coupons_used2))
    
    # 满减券 + 5元优惠券
    if full_reduction > 0 and coupons > 0:
        # 先使用满减券,再使用5元优惠券
        price1 = original_price
        coupons_used1 = 0
        num_full1 = min(full_reduction, price1 // 100)
        price1 -= num_full1 * 10
        coupons_used1 += num_full1
        num_coupon1 = min(coupons, price1 // 5)
        price1 -= num_coupon1 * 5
        coupons_used1 += num_coupon1
        if price1 < 0:
            price1 = 0
        options.append((price1, coupons_used1))
        
        # 先使用5元优惠券,再使用满减券
        price2 = original_price
        coupons_used2 = 0
        num_coupon2 = min(coupons, price2 // 5)
        price2 -= num_coupon2 * 5
        coupons_used2 += num_coupon2
        num_full2 = min(full_reduction, price2 // 100)
        price2 -= num_full2 * 10
        coupons_used2 += num_full2
        if price2 < 0:
            price2 = 0
        options.append((price2, coupons_used2))
    
    # 打折券 + 5元优惠券
    if discounts > 0 and coupons > 0:
        # 先使用打折券,再使用5元优惠券
        price1 = original_price
        coupons_used1 = 0
        price1 = math.floor(price1 * 0.92)
        coupons_used1 += 1
        num_coupon1 = min(coupons, price1 // 5)
        price1 -= num_coupon1 * 5
        coupons_used1 += num_coupon1
        if price1 < 0:
            price1 = 0
        options.append((price1, coupons_used1))
        
        # 先使用5元优惠券,再使用打折券
        price2 = original_price
        coupons_used2 = 0
        num_coupon2 = min(coupons, price2 // 5)
        price2 -= num_coupon2 * 5
        coupons_used2 += num_coupon2
        price2 = math.floor(price2 * 0.92)
        coupons_used2 += 1
        if price2 < 0:
            price2 = 0
        options.append((price2, coupons_used2))
    
    # 从所有选项中选择最优的
    for price, used in options:
        if price < min_price:
            min_price = price
            min_coupons = used
        elif price == min_price and used < min_coupons:
            min_coupons = used
    
    return min_price, min_coupons

def main():
    # 读取输入
    input_lines = sys.stdin.read().splitlines()
    # 第一行:每种优惠券的数量,按满减、打折、无门槛的顺序
    coupons = list(map(int, input_lines[0].strip().split()))
    full_reduction = coupons[0]
    discounts = coupons[1]
    coupons_5 = coupons[2]
    
    # 第二行:购物人数
    n = int(input_lines[1].strip())
    
    # 接下来n行:每个人的购物总价格
    prices = list(map(int, input_lines[2:2+n]))
    
    # 对每个人的价格进行计算
    for price in prices:
        final_price, total_coupons = calculate_final_price(price, full_reduction, discounts, coupons_5)
        print(f"{final_price} {total_coupons}")

if __name__ == "__main__":
    main()

七、JavaScript算法源码

// 导入读取输入的模块
const readline = require('readline');

// 创建接口用于读取输入
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let input = [];
rl.on('line', (line) => {
    input.push(line);
});
rl.on('close', () => {
    // 第一行:每种优惠券的数量,按满减、打折、无门槛的顺序
    let coupons = input[0].trim().split(' ').map(Number);
    let full_reduction = coupons[0]; // 满减券数量
    let discounts = coupons[1];      // 打折券数量
    let coupons_5 = coupons[2];      // 无门槛5元优惠券数量
    
    // 第二行:购物人数
    let n = parseInt(input[1].trim());
    
    // 接下来n行:每个人的购物总价格
    let prices = [];
    for(let i=2; i<2+n; i++) {
        prices.push(parseInt(input[i].trim()));
    }
    
    // 定义一个函数来计算最终价格和使用的优惠券数量
    function calculate_final_price(original_price, full_reduction, discounts, coupons_5) {
        // 初始化最优价格为原价,使用优惠券数量为0
        let min_price = original_price;
        let min_coupons = 0;
        
        // 定义一个数组来存储所有可能的选项
        let options = [];
        options.push([original_price, 0]); // 不使用任何优惠券
        
        // 使用满减券
        if(full_reduction >0){
            let price_after_full = original_price;
            let coupons_used = 0;
            // 每满100元使用1张满减券,减10元
            let num_full = Math.min(full_reduction, Math.floor(price_after_full / 100));
            price_after_full -= num_full * 10;
            coupons_used += num_full;
            if(price_after_full <0){
                price_after_full =0;
            }
            options.push([price_after_full, coupons_used]);
        }
        
        // 使用打折券
        if(discounts >0){
            let price_after_discount = Math.floor(original_price * 0.92);
            let coupons_used =1;
            options.push([price_after_discount, coupons_used]);
        }
        
        // 使用5元优惠券
        if(coupons_5 >0){
            let price_after_coupon = original_price;
            // 使用尽可能多的5元优惠券,不超过可用数量和不超过价格
            let num_coupon = Math.min(coupons_5, Math.floor(price_after_coupon /5));
            price_after_coupon -= num_coupon *5;
            let coupons_used = num_coupon;
            options.push([price_after_coupon, coupons_used]);
        }
        
        // 使用两种优惠券的组合
        // 满减券 + 打折券
        if(full_reduction >0 && discounts >0){
            // 先使用满减券,再使用打折券
            let price1 = original_price;
            let coupons_used1 =0;
            let num_full1 = Math.min(full_reduction, Math.floor(price1 /100));
            price1 -= num_full1 *10;
            coupons_used1 += num_full1;
            price1 = Math.floor(price1 *0.92);
            coupons_used1 +=1;
            if(price1 <0){
                price1 =0;
            }
            options.push([price1, coupons_used1]);
            
            // 先使用打折券,再使用满减券
            let price2 = original_price;
            let coupons_used2 =0;
            price2 = Math.floor(price2 *0.92);
            coupons_used2 +=1;
            let num_full2 = Math.min(full_reduction, Math.floor(price2 /100));
            price2 -= num_full2 *10;
            coupons_used2 += num_full2;
            if(price2 <0){
                price2 =0;
            }
            options.push([price2, coupons_used2]);
        }
        
        // 满减券 + 5元优惠券
        if(full_reduction >0 && coupons_5 >0){
            // 先使用满减券,再使用5元优惠券
            let price1 = original_price;
            let coupons_used1 =0;
            let num_full1 = Math.min(full_reduction, Math.floor(price1 /100));
            price1 -= num_full1 *10;
            coupons_used1 += num_full1;
            let num_coupon1 = Math.min(coupons_5, Math.floor(price1 /5));
            price1 -= num_coupon1 *5;
            coupons_used1 += num_coupon1;
            if(price1 <0){
                price1 =0;
            }
            options.push([price1, coupons_used1]);
            
            // 先使用5元优惠券,再使用满减券
            let price2 = original_price;
            let coupons_used2 =0;
            let num_coupon2 = Math.min(coupons_5, Math.floor(price2 /5));
            price2 -= num_coupon2 *5;
            coupons_used2 += num_coupon2;
            let num_full2 = Math.min(full_reduction, Math.floor(price2 /100));
            price2 -= num_full2 *10;
            coupons_used2 += num_full2;
            if(price2 <0){
                price2 =0;
            }
            options.push([price2, coupons_used2]);
        }
        
        // 打折券 + 5元优惠券
        if(discounts >0 && coupons_5 >0){
            // 先使用打折券,再使用5元优惠券
            let price1 = original_price;
            let coupons_used1 =0;
            price1 = Math.floor(price1 *0.92);
            coupons_used1 +=1;
            let num_coupon1 = Math.min(coupons_5, Math.floor(price1 /5));
            price1 -= num_coupon1 *5;
            coupons_used1 += num_coupon1;
            if(price1 <0){
                price1 =0;
            }
            options.push([price1, coupons_used1]);
            
            // 先使用5元优惠券,再使用打折券
            let price2 = original_price;
            let coupons_used2 =0;
            let num_coupon2 = Math.min(coupons_5, Math.floor(price2 /5));
            price2 -= num_coupon2 *5;
            coupons_used2 += num_coupon2;
            price2 = Math.floor(price2 *0.92);
            coupons_used2 +=1;
            if(price2 <0){
                price2 =0;
            }
            options.push([price2, coupons_used2]);
        }
        
        // 从所有选项中选择最优的
        for(let [price, used] of options){
            if(price < min_price){
                min_price = price;
                min_coupons = used;
            }
            else if(price === min_price && used < min_coupons){
                min_coupons = used;
            }
        }
        
        return [min_price, min_coupons];
    }
    
    // 对每个人的价格进行计算并输出结果
    for(let price of prices){
        let [final_price, total_coupons] = calculate_final_price(price, full_reduction, discounts, coupons_5);
        console.log(`${final_price} ${total_coupons}`);
    }
});

八、C算法源码

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

/**
 * 计算单个购物的最终价格和使用的优惠券数量
 * @param original_price 原始购物价格
 * @param full_reduction 满减券数量
 * @param discounts 打折券数量
 * @param coupons_5 无门槛5元优惠券数量
 * @param min_price 指向存储最优价格的指针
 * @param min_coupons 指向存储最优使用优惠券数量的指针
 */
void calculate_final_price(int original_price, int full_reduction, int discounts, int coupons_5, int *min_price, int *min_coupons){
    // 初始化最优价格为原价,使用优惠券数量为0
    *min_price = original_price;
    *min_coupons = 0;
    
    // 定义一个结构来存储所有可能的选项
    // 使用动态数组模拟
    typedef struct {
        int price;
        int used;
    } Option;
    
    Option options[12]; // 最多有12种组合
    int option_count =0;
    
    // 不使用任何优惠券
    options[option_count].price = original_price;
    options[option_count].used =0;
    option_count++;
    
    // 使用满减券
    if(full_reduction >0){
        int price_after_full = original_price;
        int coupons_used =0;
        int num_full = (full_reduction < (price_after_full /100)) ? full_reduction : (price_after_full /100);
        price_after_full -= num_full *10;
        coupons_used += num_full;
        if(price_after_full <0){
            price_after_full =0;
        }
        options[option_count].price = price_after_full;
        options[option_count].used = coupons_used;
        option_count++;
    }
    
    // 使用打折券
    if(discounts >0){
        int price_after_discount = floor(original_price *0.92);
        int coupons_used =1;
        options[option_count].price = price_after_discount;
        options[option_count].used = coupons_used;
        option_count++;
    }
    
    // 使用5元优惠券
    if(coupons_5 >0){
        int price_after_coupon = original_price;
        int num_coupon = (coupons_5 < (price_after_coupon /5)) ? coupons_5 : (price_after_coupon /5);
        price_after_coupon -= num_coupon *5;
        int coupons_used = num_coupon;
        options[option_count].price = price_after_coupon;
        options[option_count].used = coupons_used;
        option_count++;
    }
    
    // 使用两种优惠券的组合
    // 满减券 + 打折券
    if(full_reduction >0 && discounts >0){
        // 先使用满减券,再使用打折券
        int price1 = original_price;
        int coupons_used1 =0;
        int num_full1 = (full_reduction < (price1 /100)) ? full_reduction : (price1 /100);
        price1 -= num_full1 *10;
        coupons_used1 += num_full1;
        price1 = floor(price1 *0.92);
        coupons_used1 +=1;
        if(price1 <0){
            price1 =0;
        }
        options[option_count].price = price1;
        options[option_count].used = coupons_used1;
        option_count++;
        
        // 先使用打折券,再使用满减券
        int price2 = original_price;
        int coupons_used2 =0;
        price2 = floor(price2 *0.92);
        coupons_used2 +=1;
        int num_full2 = (full_reduction < (price2 /100)) ? full_reduction : (price2 /100);
        price2 -= num_full2 *10;
        coupons_used2 += num_full2;
        if(price2 <0){
            price2 =0;
        }
        options[option_count].price = price2;
        options[option_count].used = coupons_used2;
        option_count++;
    }
    
    // 满减券 + 5元优惠券
    if(full_reduction >0 && coupons_5 >0){
        // 先使用满减券,再使用5元优惠券
        int price1 = original_price;
        int coupons_used1 =0;
        int num_full1 = (full_reduction < (price1 /100)) ? full_reduction : (price1 /100);
        price1 -= num_full1 *10;
        coupons_used1 += num_full1;
        int num_coupon1 = (coupons_5 < (price1 /5)) ? coupons_5 : (price1 /5);
        price1 -= num_coupon1 *5;
        coupons_used1 += num_coupon1;
        if(price1 <0){
            price1 =0;
        }
        options[option_count].price = price1;
        options[option_count].used = coupons_used1;
        option_count++;
        
        // 先使用5元优惠券,再使用满减券
        int price2 = original_price;
        int coupons_used2 =0;
        int num_coupon2 = (coupons_5 < (price2 /5)) ? coupons_5 : (price2 /5);
        price2 -= num_coupon2 *5;
        coupons_used2 += num_coupon2;
        int num_full2 = (full_reduction < (price2 /100)) ? full_reduction : (price2 /100);
        price2 -= num_full2 *10;
        coupons_used2 += num_full2;
        if(price2 <0){
            price2 =0;
        }
        options[option_count].price = price2;
        options[option_count].used = coupons_used2;
        option_count++;
    }
    
    // 打折券 + 5元优惠券
    if(discounts >0 && coupons_5 >0){
        // 先使用打折券,再使用5元优惠券
        int price1 = original_price;
        int coupons_used1 =0;
        price1 = floor(price1 *0.92);
        coupons_used1 +=1;
        int num_coupon1 = (coupons_5 < (price1 /5)) ? coupons_5 : (price1 /5);
        price1 -= num_coupon1 *5;
        coupons_used1 += num_coupon1;
        if(price1 <0){
            price1 =0;
        }
        options[option_count].price = price1;
        options[option_count].used = coupons_used1;
        option_count++;
        
        // 先使用5元优惠券,再使用打折券
        int price2 = original_price;
        int coupons_used2 =0;
        int num_coupon2 = (coupons_5 < (price2 /5)) ? coupons_5 : (price2 /5);
        price2 -= num_coupon2 *5;
        coupons_used2 += num_coupon2;
        price2 = floor(price2 *0.92);
        coupons_used2 +=1;
        if(price2 <0){
            price2 =0;
        }
        options[option_count].price = price2;
        options[option_count].used = coupons_used2;
        option_count++;
    }
    
    // 从所有选项中选择最优的
    for(int i=0; i<option_count; i++){
        if(options[i].price < *min_price){
            *min_price = options[i].price;
            *min_coupons = options[i].used;
        }
        else if(options[i].price == *min_price && options[i].used < *min_coupons){
            *min_coupons = options[i].used;
        }
    }
}

int main(){
    // 读取第一行:每种优惠券的数量
    int full_reduction, discounts, coupons_5;
    scanf("%d %d %d", &full_reduction, &discounts, &coupons_5);
    
    // 读取第二行:购物人数
    int n;
    scanf("%d", &n);
    
    // 读取接下来的n行:每个人的购物价格
    int prices[n];
    for(int i=0; i<n; i++){
        scanf("%d", &prices[i]);
    }
    
    // 对每个人的价格进行计算并输出结果
    for(int i=0; i<n; i++){
        int final_price, total_coupons;
        calculate_final_price(prices[i], full_reduction, discounts, coupons_5, &final_price, &total_coupons);
        printf("%d %d\n", final_price, total_coupons);
    }
    
    return 0;
}

九、C++算法源码

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;

/**
 * 计算单个购物的最终价格和使用的优惠券数量
 * @param original_price 原始购物价格
 * @param full_reduction 满减券数量
 * @param discounts 打折券数量
 * @param coupons_5 无门槛5元优惠券数量
 * @return pair<int, int> 最终价格和使用的优惠券总数量
 */
pair<int, int> calculate_final_price(int original_price, int full_reduction, int discounts, int coupons_5){
    // 初始化最优价格为原价,使用优惠券数量为0
    int min_price = original_price;
    int min_coupons = 0;
    
    // 定义一个结构来存储所有可能的选项
    vector<pair<int, int>> options;
    options.emplace_back(original_price, 0); // 不使用任何优惠券
    
    // 使用满减券
    if(full_reduction >0){
        int price_after_full = original_price;
        int coupons_used =0;
        // 每满100元使用1张满减券,减10元
        int num_full = min(full_reduction, price_after_full /100);
        price_after_full -= num_full *10;
        coupons_used += num_full;
        if(price_after_full <0){
            price_after_full =0;
        }
        options.emplace_back(price_after_full, coupons_used);
    }
    
    // 使用打折券
    if(discounts >0){
        int price_after_discount = floor(original_price *0.92);
        int coupons_used =1;
        options.emplace_back(price_after_discount, coupons_used);
    }
    
    // 使用5元优惠券
    if(coupons_5 >0){
        int price_after_coupon = original_price;
        // 使用尽可能多的5元优惠券,不超过可用数量和不超过价格
        int num_coupon = min(coupons_5, price_after_coupon /5);
        price_after_coupon -= num_coupon *5;
        int coupons_used = num_coupon;
        options.emplace_back(price_after_coupon, coupons_used);
    }
    
    // 使用两种优惠券的组合
    // 满减券 + 打折券
    if(full_reduction >0 && discounts >0){
        // 先使用满减券,再使用打折券
        int price1 = original_price;
        int coupons_used1 =0;
        int num_full1 = min(full_reduction, price1 /100);
        price1 -= num_full1 *10;
        coupons_used1 += num_full1;
        price1 = floor(price1 *0.92);
        coupons_used1 +=1;
        if(price1 <0){
            price1 =0;
        }
        options.emplace_back(price1, coupons_used1);
        
        // 先使用打折券,再使用满减券
        int price2 = original_price;
        int coupons_used2 =0;
        price2 = floor(price2 *0.92);
        coupons_used2 +=1;
        int num_full2 = min(full_reduction, price2 /100);
        price2 -= num_full2 *10;
        coupons_used2 += num_full2;
        if(price2 <0){
            price2 =0;
        }
        options.emplace_back(price2, coupons_used2);
    }
    
    // 满减券 + 5元优惠券
    if(full_reduction >0 && coupons_5 >0){
        // 先使用满减券,再使用5元优惠券
        int price1 = original_price;
        int coupons_used1 =0;
        int num_full1 = min(full_reduction, price1 /100);
        price1 -= num_full1 *10;
        coupons_used1 += num_full1;
        int num_coupon1 = min(coupons_5, price1 /5);
        price1 -= num_coupon1 *5;
        coupons_used1 += num_coupon1;
        if(price1 <0){
            price1 =0;
        }
        options.emplace_back(price1, coupons_used1);
        
        // 先使用5元优惠券,再使用满减券
        int price2 = original_price;
        int coupons_used2 =0;
        int num_coupon2 = min(coupons_5, price2 /5);
        price2 -= num_coupon2 *5;
        coupons_used2 += num_coupon2;
        int num_full2 = min(full_reduction, price2 /100);
        price2 -= num_full2 *10;
        coupons_used2 += num_full2;
        if(price2 <0){
            price2 =0;
        }
        options.emplace_back(price2, coupons_used2);
    }
    
    // 打折券 + 5元优惠券
    if(discounts >0 && coupons_5 >0){
        // 先使用打折券,再使用5元优惠券
        int price1 = original_price;
        int coupons_used1 =0;
        price1 = floor(price1 *0.92);
        coupons_used1 +=1;
        int num_coupon1 = min(coupons_5, price1 /5);
        price1 -= num_coupon1 *5;
        coupons_used1 += num_coupon1;
        if(price1 <0){
            price1 =0;
        }
        options.emplace_back(price1, coupons_used1);
        
        // 先使用5元优惠券,再使用打折券
        int price2 = original_price;
        int coupons_used2 =0;
        int num_coupon2 = min(coupons_5, price2 /5);
        price2 -= num_coupon2 *5;
        coupons_used2 += num_coupon2;
        price2 = floor(price2 *0.92);
        coupons_used2 +=1;
        if(price2 <0){
            price2 =0;
        }
        options.emplace_back(price2, coupons_used2);
    }
    
    // 从所有选项中选择最优的
    for(auto &option: options){
        int price = option.first;
        int used = option.second;
        if(price < min_price){
            min_price = price;
            min_coupons = used;
        }
        else if(price == min_price && used < min_coupons){
            min_coupons = used;
        }
    }
    
    return make_pair(min_price, min_coupons);
}

int main(){
    ios::sync_with_stdio(false); // 关闭同步,提高输入速度
    cin.tie(0); // 取消cin和cout的绑定
    
    // 读取第一行:每种优惠券的数量
    int full_reduction, discounts, coupons_5;
    cin >> full_reduction >> discounts >> coupons_5;
    
    // 读取第二行:购物人数
    int n;
    cin >> n;
    
    // 读取接下来的n行:每个人的购物价格
    vector<int> prices(n);
    for(int i=0; i<n; i++){
        cin >> prices[i];
    }
    
    // 对每个人的价格进行计算并输出结果
    for(int i=0; i<n; i++){
        pair<int, int> result = calculate_final_price(prices[i], full_reduction, discounts, coupons_5);
        cout << result.first << " " << result.second << "\n";
    }
    
    return 0;
}


🏆下一篇:华为OD机试真题 - 简易内存池(Python/JS/C/C++ 2024 E卷 200分)

🏆本文收录于,华为OD机试真题(Python/JS/C/C++)

刷的越多,抽中的概率越大,私信哪吒,备注华为OD,加入华为OD刷题交流群,每一题都有详细的答题思路、详细的代码注释、3个测试用例、为什么这道题采用XX算法、XX算法的适用场景,发现新题目,随时更新,全天CSDN在线答疑。

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2180671.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

CHI write 传输——CHI(5)

上篇介绍了dataless的操作类型&#xff0c;本篇我们来介绍一下write 一、Write 操作概览 cache stash &#xff1a;一种投机行为&#xff0c;通过在其未来的使用点附近分配一个cacheline来提高系统性能&#xff0c;因为可以减少使用数据时的内存访问延迟 二、CopyBack CopyB…

CRM如何助力企业内部高效管理?

企业内部的高效管理不仅是提升竞争力的关键&#xff0c;也是实现企业可持续发展的基石。客户关系管理&#xff08;CRM&#xff09;系统&#xff0c;作为连接客户与企业内部流程的重要桥梁&#xff0c;其在促进企业内部高效管理方面的作用日益凸显。通过自动化工作流程、跨部门信…

19.第二阶段x86游戏实战2-寻找寻路call

免责声明&#xff1a;内容仅供学习参考&#xff0c;请合法利用知识&#xff0c;禁止进行违法犯罪活动&#xff01; 本次游戏没法给 内容参考于&#xff1a;微尘网络安全 本人写的内容纯属胡编乱造&#xff0c;全都是合成造假&#xff0c;仅仅只是为了娱乐&#xff0c;请不要…

使用powershell的脚本报错:因为在此系统中禁止执行脚本

1.添加powershell功能环境&#xff1a; 2.启动powershell的执行策略 因为在此系统中禁止执行脚本。 set-executionpolicy unrestricted

leetcode每日一题day19(24.9.29)——买票需要的时间

思路&#xff1a;在最开始的情况下每人需要买的票数减一是能保持相对位置不变的&#xff0c; 如果再想减一就有可能 有某些人只买一张票&#xff0c;而离开了队伍&#xff0c; 所有容易想到对于某个人如果比当前的人买的多就按当前的人数量算 因为在一次次减一的情况下&#xf…

风险函数梳理工具

风险函数梳理工具 在日常的软件开发工作中&#xff0c;代码的安全性和质量至关重要。然而&#xff0c;面对庞大的代码库&#xff0c;手动查找潜在的风险函数不仅耗时&#xff0c;而且容易出错。特别是在团队协作中&#xff0c;代码审查和重构工作往往占据了大量宝贵的时间&…

【机器学习】---异构数据融合

文章目录 1. 引言2. 异构数据融合的概念3. 常用的异构数据融合技术3.1 早期融合&#xff08;Early Fusion&#xff09;3.2 晚期融合&#xff08;Late Fusion&#xff09;3.3 中期融合&#xff08;Intermediate Fusion&#xff09;3.4 递归融合&#xff08;Recursive Fusion&…

JavaSE——进制转换、原码、反码、补码、位运算(左移、右移、取反)

目录 一、四种进制介绍 二、进制的转换 (一)二进制—>十进制 (二)八进制—>十进制 (三)十六进制—>十进制 (四)十进制—>二进制 (五)十进制—>八进制 (六)十进制—>十六进制 (七)二进制—>八进制 (八)二进制—>十六进制 (九)八进制—>二…

免费升级!Windows10 64位国庆特别版:包含最新安全补丁!

国庆佳节临近&#xff0c;系统之家小编给大家带来了Windows10 64位国庆特别版&#xff0c;该版本系统以最新Windows10 22H2专业版系统为基础&#xff0c;展开离线制作与优化&#xff0c;安全无毒&#xff0c;且运作更加流畅稳定&#xff0c;建议大家升级。Windows10 64位国庆特…

华为OD机试 - 三阶积幻方(Java 2024 E卷 100分)

华为OD机试 2024E卷题库疯狂收录中&#xff0c;刷题点这里 专栏导读 本专栏收录于《华为OD机试&#xff08;JAVA&#xff09;真题&#xff08;E卷D卷A卷B卷C卷&#xff09;》。 刷的越多&#xff0c;抽中的概率越大&#xff0c;私信哪吒&#xff0c;备注华为OD&#xff0c;加…

长沙惠科成功完成SAP S/4 1511到S/4 2021的无缝升级,实现“停机不停产”

关于HKC长沙惠科光电有限公司 长沙惠科光电有限公司于2019年9月20日落户于长沙市浏阳经济技术开发区&#xff0c;注册资本220亿元人民币&#xff0c;由惠科股份有限公司与湖南金阳投资集团、浏阳市城市建设集团有限公司共同出资&#xff0c;是惠科股份有限公司下属的第四家专业…

WebAssembly 为什么能提升性能,怎么使用它 ?

文章目录 简介&#xff1a;起源&#xff1a;前端性能提升历史JIT&#xff08;Just-In-Time&#xff09;编译器(即时编译) 为什么需要WebAssembly&#xff1a;WebAssembly能做什么&#xff1a;经常说WASM的性能高&#xff0c;为什么高&#xff1f;&#xff1f;使用方法:Emscript…

《OpenCV 计算机视觉》—— 视频背景建模

还未写完&#xff01;&#xff01;&#xff01; 完整代码如下&#xff1a; import cv2cap cv2.VideoCapture(test.avi)""" getstructuringElement(shape&#xff0c;ksize,anchorNone)得到一个卷积核。主要用于后续的腐蚀、膨胀、开、闭等运算。 参数:shape:设…

Redis进阶篇 - 缓存穿透、缓存击穿、缓存雪崩问题及其解决方案

文章目录 1 文章概述2 缓存穿透2.1 什么是缓存穿透&#xff1f;2.2 缓存穿透的解决方法2.2.1 做好参数校验2.2.2 缓存无效Key2.2.3 使用布隆过滤器2.2.4 接口限流 3 缓存击穿3.1 什么是缓存击穿&#xff1f;3.2 缓存击穿的解决方法3.2.1 调整热点数据过期时间3.2.2 热点数据预热…

docker环境下配置cerbot获取免费ssl证书并自动续期

文章目录 实践场景了解certbot查看nginx的映射情况操作目标配置nginx配置的ssl证书设置自动续签 实践场景 本人使用docker部署了一个nginx容器&#xff0c;通过容器卷&#xff0c;实现本地html&#xff0c;ssl&#xff0c;conf和ngiinx容器映射的&#xff0c; 经常需要手动部署…

WPS在表格中填写材料时,内容过多导致表格不换页,其余内容无法正常显示 以及 内容过多,导致表格换页——解决方法

一、现象 1&#xff0c;内容过多导致表格不换页&#xff0c;其余内容无法正常显示 2&#xff0c;内容过多&#xff0c;导致表格换页 二、解决方法 在表格内右击&#xff0c;选择表格属性 在菜单栏选择行&#xff0c;勾选允许跨页断行&#xff0c;点击确定即可 1&#xff0…

react+antdMobie实现消息通知页面样式

一、实现效果 二、代码 import React, { useEffect, useState } from react; import style from ./style/index.less; import { CapsuleTabs, Ellipsis, Empty, SearchBar, Tag } from antd-mobile; //消息通知页面 export default function Notification(props) {const [opti…

Kafka学习笔记(三)Kafka分区和副本机制、自定义分区、消费者指定分区

文章目录 前言7 分区和副本机制7.1 生产者分区写入策略7.1.1 轮询分区策略7.1.2 随机分区策略7.1.3 按key分区分配策略7.1.4 自定义分区策略7.1.4.1 实现Partitioner接口7.1.4.2 实现分区逻辑7.1.4.3 配置使用自定义分区器7.1.4.4 分区测试 7.2 消费者分区分配策略7.2.1 RangeA…

【MySQL】常见的SQL优化方式(二)

目录 1、limit 优化 &#xff08;1&#xff09;延迟关联&#xff08;索引覆盖子查询&#xff09; &#xff08;2&#xff09; 已知位置查询 2、group by 优化 &#xff08;1&#xff09;使用索引 &#xff08;2&#xff09;避免排序 &#xff08;3&#xff09;分析查询 …

LeetCode[中等] 739. 每日温度

给定一个整数数组 temperatures &#xff0c;表示每天的温度&#xff0c;返回一个数组 answer &#xff0c;其中 answer[i] 是指对于第 i 天&#xff0c;下一个更高温度出现在几天后。如果气温在这之后都不会升高&#xff0c;请在该位置用 0 来代替。 思路 栈 暴力法为遍历列…