2024华为OD试题及答案-A041-网上商城优惠活动

题目描述

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

  • 每满100元优惠10元,无使用数限制,如100~199元可以使用1张减10元,200~299可使用2张减20元,以此类推;
  • 92折券,1次限使用1张,如100元,则优惠后为92元;
  • 无门槛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. 优惠后价格如果是小数,则向下取整,输出都为整数。

用例
输入3 2 5
3
100
200
400
输出65 6
155 7
338 4
说明

输入:

  • 第一行:3种优惠券数量分别为:满减券3张,打折券2张,无门槛5张
  • 第二行:总共3个人购物
  • 第三行:第一个人购物优惠前价格为100元
  • 第四行:第二个人购物优惠前价格为200元
  • 第五行:第三个人购物优惠前价格为400元

输入3个人,输出3行结果,同输入的顺序,对应每个人的优惠结果,如下:

  • 第一行输出:先使用1张满减券优惠到90元,再使用5张无门槛券优惠到25元,最终价格是65元,总共使用6张优惠券。
  • 第二行输出:先使用2张满减券优惠到180元,再使用5张无门槛券优惠到25元,最终价格是155元,总共使用7张优惠券。
  • 第三行输出:先使用1张92折券优惠到368元,再使用3张满减券优惠到30元,最终价格是338元,总共使用4张优惠券。

解题思路分析

题目要求设计一种方法,帮助每个顾客以最优的方式使用优惠券,使得他们的购物价格最低。优惠券有三种:满减、打折和无门槛券。需要考虑每个人使用优惠券的顺序,最大化优惠效果。

优惠券规则
  1. 满减:每满100元减10元。
  2. 打折:92折。
  3. 无门槛:直接减5元。
优惠券使用限制
  • 每次只能使用一种满减和一种打折优惠券。
  • 无门槛优惠券可以叠加使用。
  • 优惠券的使用顺序会影响最终价格。
解决思路
  1. 输入

    • 每个人拥有的优惠券数量。
    • 每个人的购物总价。
  2. 输出

    • 每个人每次购物使用的最少优惠券数量。
    • 每个人的最终价格。

步骤

  1. 读取输入数据。
  2. 对每个人的购物价格分别计算使用不同顺序的优惠券后的最低价格。
  3. 计算并输出每个人使用的优惠券数量和最终价格。

C++程序实现

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

struct Person {
    int fullReduction;
    int discount;
    int noThreshold;
    int originalPrice;
    int minPrice;
    int couponCount;
};

int applyFullReduction(int price, int fullReductionCount) {
    return price - (price / 100) * 10 * fullReductionCount;
}

int applyDiscount(int price, int discountCount) {
    return price * pow(0.92, discountCount);
}

int applyNoThreshold(int price, int noThresholdCount) {
    return price - 5 * noThresholdCount;
}

void calculateMinPrice(Person &person) {
    int bestPrice = person.originalPrice;
    int minCoupons = 0;

    for (int i = 0; i <= person.fullReduction; ++i) {
        for (int j = 0; j <= person.discount; ++j) {
            for (int k = 0; k <= person.noThreshold; ++k) {
                int tempPrice = person.originalPrice;
                tempPrice = applyFullReduction(tempPrice, i);
                tempPrice = applyDiscount(tempPrice, j);
                tempPrice = applyNoThreshold(tempPrice, k);

                if (tempPrice < bestPrice) {
                    bestPrice = tempPrice;
                    minCoupons = i + j + k;
                }
            }
        }
    }

    person.minPrice = bestPrice;
    person.couponCount = minCoupons;
}

int main() {
    int n;
    cin >> n;

    vector<Person> persons(n);
    for (int i = 0; i < n; ++i) {
        cin >> persons[i].fullReduction >> persons[i].discount >> persons[i].noThreshold;
    }

    for (int i = 0; i < n; ++i) {
        cin >> persons[i].originalPrice;
    }

    for (int i = 0; i < n; ++i) {
        calculateMinPrice(persons[i]);
    }

    for (int i = 0; i < n; ++i) {
        cout << persons[i].couponCount << " " << persons[i].minPrice << endl;
    }

    return 0;
}

Python程序实现

 
import math

def apply_full_reduction(price, full_reduction_count):
    return price - (price // 100) * 10 * full_reduction_count

def apply_discount(price, discount_count):
    return price * (0.92 ** discount_count)

def apply_no_threshold(price, no_threshold_count):
    return price - 5 * no_threshold_count

def calculate_min_price(person):
    best_price = person['original_price']
    min_coupons = 0

    for i in range(person['full_reduction'] + 1):
        for j in range(person['discount'] + 1):
            for k in range(person['no_threshold'] + 1):
                temp_price = person['original_price']
                temp_price = apply_full_reduction(temp_price, i)
                temp_price = apply_discount(temp_price, j)
                temp_price = apply_no_threshold(temp_price, k)

                if temp_price < best_price:
                    best_price = temp_price
                    min_coupons = i + j + k

    person['min_price'] = best_price
    person['coupon_count'] = min_coupons

def main():
    n = int(input().strip())

    persons = []
    for _ in range(n):
        full_reduction, discount, no_threshold = map(int, input().strip().split())
        persons.append({
            'full_reduction': full_reduction,
            'discount': discount,
            'no_threshold': no_threshold,
            'original_price': 0,
            'min_price': 0,
            'coupon_count': 0
        })

    for i in range(n):
        persons[i]['original_price'] = int(input().strip())

    for person in persons:
        calculate_min_price(person)

    for person in persons:
        print(person['coupon_count'], person['min_price'])

if __name__ == "__main__":
    main()

解释

上述代码首先读取输入数据,然后对每个顾客的购物价格进行计算,找出使用优惠券后的最低价格,并记录使用的优惠券数量。最终输出每个人的优惠券数量和最低价格。C++和Python实现的逻辑一致,只是语法有所不同。

  • 16
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值