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

在这里插入图片描述

华为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在线答疑。

在这里插入图片描述

华为OD机试-2023真题的考点主要为以下几个类: 1. 数据结构与算法:考察对各种常用数据结构(如数组、链表、栈、队列、树、图等)和算法(如排序、查找、递归、动态规划等)的理解和应用能力,以及对时间复杂度和空间复杂度的析和优化。 2. 编程语言和语法:考察对编程语言(如C++Python等)的基本语法和特性的掌握程度,包括变量、运算符、控制流程、函数、类等。同时还要求考生熟练运用相关的标准库和常用的数据结构和算法。 3. 网络与操作系统:考察对计算机网络和操作系统的基本原理和常用技术的了解,包括网络通信、TCP/IP协议、进程管理、内存管理、文件系统等。要求考生理解并能解决相关的问题。 4. 数据库与SQL:考察对数据库的基本概念和原理的理解,包括数据模型、关系代数、SQL语言等内容。要求考生能够编写和优化SQL查询语句,处理常见的数据库操作和性能问题。 5. 系统设计与架构:考察对大型系统的设计和架构的理解,包括系统需求析、模块划、接口设计等。要求考生能够根据需求或场景提出可行的系统设计方案,并能解决相关的实际问题。 总体来说,华为OD机试-2023真题的考点比较全面,涵盖了计算机科学与技术的多个方面。考生需要具备扎实的数据结构与算法基础,熟悉常用编程语言和标准库的使用,了解网络和操作系统的基本原理,掌握数据库和SQL语言的基本知识,同时具备系统设计和架构的能力。只有在这些方面的基础上才能够应对各种考题,并取得优异的表现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哪 吒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值