小猫吃鱼:fish.cpp

博客讨论了一个使用C++编程解决的问题,即如何计算在经过多个站点时,为小猫提供鱼的最小总费用,考虑了鱼的价格和保存鱼的电能成本,输入包括站点数和各站费用,输出是最小总费用。

题目描述
原理:
明明家从 1 号站点出发,开车去旅游,一共要经过 n 个站点,依次为 2、3……n。
由于明明带上了心爱的小猫,在每个站点都要为小猫提供一条鱼用做美餐(包括 1 号站点)。
除了 1 号站点只能吃 1 号站点买的鱼,其他站点既可以吃当地买的鱼,也可吃之前经过的站点买了存入车载冰箱中的鱼。
但车载冰箱消耗的电能来自汽油,所以每条鱼用冰箱保存到下一站的费用与各个站点的汽油价格有关。
为使问题简化,我们约定:
(1)车从某站开出时油箱中都是此站点刚加的汽油。
(2)车载冰箱能容纳一路上需要的所有鱼。
即:每条鱼的费用既包括购买时的费用,也包括用冰箱保存鱼的费用。

编程实现:
为了降低小猫吃鱼的总代价,明明预先上网查到了这 n 个站点的鱼价和汽油价格。并据此算出每个站点买一条鱼的费用以及从该站点到下一站用冰箱保存一条鱼的费用。你能帮明明算出这一路上小猫吃鱼的最小总费用吗?
输入
第一行:站点数 n,1<n<100。
接下来的 n 行:每行两个以空格分隔的正整数,表示:这一站买一条鱼的费用,以及从这一站把每条鱼保存到下一站的费用,两个费用均为小于 10000 的正整数。
输出
最小总费用,是一个正整数。
样例输入复制
5
6 3
7 1
3 2
8 3
9 5
样例输出复制
29

#include <iostream>
 
使用C++开发小猫游戏可以从两个不同场景实现,以下分别给出对应代码示例。 ### 场景一:小猫经验等级游戏 此场景中,小猫有等级和经验分,不同的对应不同的经验分或等级变化规则。 ```cpp #include <iostream> // 定义的基类 class Fish { public: virtual ~Fish() {} virtual void affectCat(int& level, int& exp) = 0; }; // 章类 class Octopus : public Fish { private: int weight; public: Octopus(int w) : weight(w) {} void affectCat(int& level, int& exp) override { exp += 2 * weight; while (exp >= 500) { level++; exp -= 500; } } }; // 鲸类 class Whale : public Fish { public: void affectCat(int& level, int& exp) override { exp += 200; while (exp >= 500) { level++; exp -= 500; } } }; // 金龟类 class Turtle : public Fish { public: void affectCat(int& level, int& exp) override { level++; exp = 0; } }; // 鲨类 class Shark : public Fish { private: int weight; public: Shark(int w) : weight(w) {} void affectCat(int& level, int& exp) override { int decrease = 5 * weight; exp = (exp - decrease > 0)? exp - decrease : 0; } }; // 小猫类 class Cat { private: int level; int exp; public: Cat() : level(1), exp(0) {} void fish(Fish* fish) { fish->affectCat(level, exp); delete fish; } void showStatus() { std::cout << "Level: " << level << ", Exp: " << exp << std::endl; } }; int main() { Cat cat; cat.fish(new Octopus(10)); cat.showStatus(); cat.fish(new Whale()); cat.showStatus(); cat.fish(new Turtle()); cat.showStatus(); cat.fish(new Shark(15)); cat.showStatus(); return 0; } ``` ### 场景二:扑克牌小猫游戏 此场景中,两人交替出扑克牌,若打出的牌与桌上某张牌面相同,则取走相关牌。 ```cpp #include <iostream> #include <queue> #include <vector> #include <algorithm> // 查找牌面相同的位置 int findSameCard(const std::vector<int>& table, int card) { for (int i = table.size() - 1; i >= 0; --i) { if (table[i] == card) { return i; } } return -1; } int main() { std::queue<int> player1; std::queue<int> player2; std::vector<int> table; // 初始化玩手牌,这里简单假设玩各有部分牌 for (int i = 1; i <= 5; ++i) { player1.push(i); player2.push(i + 5); } while (!player1.empty() && !player2.empty()) { // 玩1出牌 int card1 = player1.front(); player1.pop(); table.push_back(card1); int index1 = findSameCard(table, card1); if (index1 != -1) { for (int i = index1; i < table.size(); ++i) { player1.push(table[i]); } table.erase(table.begin() + index1, table.end()); } if (player1.empty()) break; // 玩2出牌 int card2 = player2.front(); player2.pop(); table.push_back(card2); int index2 = findSameCard(table, card2); if (index2 != -1) { for (int i = index2; i < table.size(); ++i) { player2.push(table[i]); } table.erase(table.begin() + index2, table.end()); } } if (player1.empty()) { std::cout << "Player 2 wins!" << std::endl; } else { std::cout << "Player 1 wins!" << std::endl; } return 0; } ``` ### 代码解释 - **场景一**:定义了一个`Fish`基类,各种继承该基类并实现`affectCat`方法,`Cat`类包含等级和经验分,通过`fish`方法钓并更新状态。 - **场景二**:使用队列存储玩手牌,向量存储桌上的牌,在循环中玩交替出牌,通过`findSameCard`函数查找相同牌面的位置,若找到则取走相关牌。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值