UVA 12325 Zombie's Treasure Chest


题目大意:

你有一个体积为N的箱子和两种数量无限的宝物。宝物1的体积为S1,价值为V1;宝物2的体积为S2,价值为V2。输入均为32位带符号的整数。你的任务是最多能装多少价值的宝物?

对于宝物1和2,当所占体积为S1*S2时,分别能够提供的价值为S2*V1和S1*V2,我们便可以根据两者的大小关系判断选哪一种物品,如果S2*V1>=S1*V2,则我们可以得出宝物的数量最多为S1-1,因为如果我们选了S1件宝物2,则我们完全可以用S2件宝物1去代替,而且我们获得价值会更大。因此每次可以枚举min(S1-1,cnt2)或者min(S2-1,cnt1)便可以得出答案。

另外书上提出了分类枚举的方法,我们可以根据S1和S2的范围大小而采取不同的枚举方法。


方法1代码:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

int main() {

    //freopen("aa.in", "r", stdin);

    int T;
    ll N, S1, V1, S2, V2;
    ll ans ;
    ll cnt1, cnt2;
    int kcase = 0;
    cin >> T;
    while(T--) {
        kcase++;
        cin >> N >> S1 >> V1 >> S2 >> V2;
        cnt1 = N / S1;
        cnt2 = N / S2;
        ans = 0;
        if(V1 * S2 >= V2 * S1) {
                for(int i = 0; i <= min(S1 - 1, cnt2); ++i) {
                    ll t = V2 * i + (N - S2 * i) / S1 * V1;
                    if(ans < t) {
                        ans = t;
                    }
                }
        } else {
                for(int i = 0; i <= min(S2-1, cnt1); ++i) {
                    ll t = V1 * i + (N - S1 * i) / S2 * V2;
                    if(ans < t) {
                        ans = t;
                    }
                }
        }
        cout << "Case #" << kcase << ": " << ans << endl;
    }
    return 0;
}

方法2代码:

/*
int main() {

    //freopen("aa.in", "r", stdin);

    int T;
    ll N, S1, V1, S2, V2;
    ll ans = 0;
    ll cnt1, cnt2;
    int kcase = 0;
    cin >> T;
    while(T--) {
        kcase++;
        cin >> N >> S1 >> V1 >> S2 >> V2;
        cnt1 = N / S1;
        cnt2 = N / S2;
        ans = 0;
        if(S1 >= 100000) {
            for(int i = 0; i <= cnt1; ++i) {
                ll t = V1 * i + (N - S1 * i) / S2 * V2;
                if(ans < t) {
                    ans = t;
                }
            }
        } else if(S2 >= 100000) {
            for(int i = 0; i <= cnt2; ++i) {
                ll t = V2 * i + (N - S2 * i) / S1 * V1;
                if(ans < t) {
                    ans = t;
                }
            }
        } else {
            if(S2 * V1 >= S1 * V2) {
                for(int i = 0; i <= min(S1 - 1, cnt2); ++i) {
                    ll t = V2 * i + (N - S2 * i) / S1 * V1;
                    if(ans < t) {
                        ans = t;
                    }
                }
            } else {
                for(int i = 0; i <= min(S2 - 1, cnt1); ++i) {
                    ll t = V1 * i + (N - S1 * i) / S2 * V2;
                    if(ans < t) {
                        ans = t;
                    }
                }
            }
        }
        cout << "Case #" << kcase << ": " << ans << endl;
    }
    return 0;
}
*/


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,让我们来创建一个用 TypeScript 编写的 Zombie Apocalypse 游戏。 首先,我们需要定义一些游戏中会用到的类型。例如,我们需要定义一个 `Player` 类型来代表游戏中的玩家: ```typescript interface Player { name: string; health: number; attack: number; defense: number; } ``` 接下来,我们需要定义一个 `Zombie` 类型来代表游戏中的僵尸: ```typescript interface Zombie { health: number; attack: number; defense: number; } ``` 然后,我们需要定义一个 `Game` 类来管理游戏状态和逻辑: ```typescript class Game { private player: Player; private zombies: Zombie[]; constructor(player: Player, zombies: Zombie[]) { this.player = player; this.zombies = zombies; } play() { while (this.player.health > 0 && this.zombies.length > 0) { const zombie = this.zombies[0]; const playerDamage = this.player.attack - zombie.defense; const zombieDamage = zombie.attack - this.player.defense; if (playerDamage > 0) { zombie.health -= playerDamage; } if (zombie.health <= 0) { this.zombies.shift(); } if (zombieDamage > 0) { this.player.health -= zombieDamage; } } if (this.player.health > 0) { console.log('You win!'); } else { console.log('You lose!'); } } } ``` 最后,我们可以创建一个游戏实例并开始游戏: ```typescript const player: Player = { name: 'John', health: 100, attack: 10, defense: 5, }; const zombies: Zombie[] = [ { health: 20, attack: 5, defense: 2 }, { health: 30, attack: 7, defense: 3 }, { health: 40, attack: 10, defense: 4 }, ]; const game = new Game(player, zombies); game.play(); ``` 这就是用 TypeScript 编写的 Zombie Apocalypse 游戏。当然,你可以根据自己的喜好和需求对代码进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值