华为机考真题 -- Wonderland 游乐园

题目描述:

Wonderland 是小王居住地一家很受欢迎的游乐园。Wonderland 目前有 4 种售票方式,分别为一日票(1 天)、三日票(3 天)、周票(7 天)和月票(30 天)。每种售票方式的价格将由一个数组给出,每种票据在票面时间内可以无限制的进行游玩。例如,小王在第 10 日买了一张三日票,小王可以在第 10 日、第 11 日和第 12 日进行无限制的游玩。

小王计划在接下来一年内多次游玩该游乐园。小王计划的游玩日期将由一个数组给出。 现在,请您根据给出的售票价格数组和小王计划游玩日期数组,返回完成游玩计划所需要的最低消费。

输入描述:

输入为 2 个数组
数组1:售票价格数组为 costs,costs.length=4,默认顺序为一日票、三日票、周票和月票。
数组2:小王计划游玩日期数组为 days,1<=days.length<=365,1<=days[i]<=365,默认顺序为升序。

输出描述:

完成游玩计划的最低消费

示例1:

输入
5 14 30 100
1 3 15 20 21 200 202 230

输出
40

说明:
根据售票价格数组和游玩日期数组给出的信息,发现每次去玩的时候买一张一日票是最省钱的,所以小王会买 8 张一日票,每张 5 元,最低花费是 40 元。

C++源码:

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <sstream>
#include <climits> // 引入INT_MAX
using namespace std;

// 计算最低游玩成本的函数
int calculateMinimumCost(const vector<int>& cost, const vector<int>& trivalDays) {
    int maxDay = trivalDays.back();
    vector<int> playDay(400, -1);
    for (int D : trivalDays)
        playDay[D] = 1;

    vector<int> costDay(400, INT_MAX);
    costDay[0] = 0;

    for (int i = 1; i <= maxDay; ++i) {
        if (playDay[i] == -1)
            costDay[i] = costDay[i - 1];
        else {
            costDay[i] = min(costDay[i], costDay[i - 1] + cost[0]);
            if (i >= 3)
                costDay[i] = min(costDay[i], costDay[i - 3] + cost[1]);
            if (i >= 7)
                costDay[i] = min(costDay[i], costDay[i - 7] + cost[2]);
            if (i >= 30)
                costDay[i] = min(costDay[i], costDay[i - 30] + cost[3]);
        }
    }

    return costDay[maxDay];
}

int main() {
    vector<int> cost, trivalDays;
    string input;

    // 输入数组cost
    getline(cin, input);
    stringstream ss1(input);
    int num;
    while (ss1 >> num) {
        cost.push_back(num);
    }

    // 输入数组trivalDays
    getline(cin, input);
    stringstream ss2(input);
    while (ss2 >> num) {
        trivalDays.push_back(num);
    }

    int costMin = calculateMinimumCost(cost, trivalDays);

    cout << costMin << endl; // 前n天游玩的最低花费

    system("pause");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值