Sheep_Li在打完训练赛后总是喜欢开一把王者,她最近练上了一个新英雄:不知火舞。为了提高二技能扇子的伤害,后期她会把所有其他装备卖掉换成贤者之书或者博学者之怒。 现在问题来了:假设当前她有N枚金币,她需要用这些金币去购买下面两种装备: 贤者之书:2990金币,获得400法术攻击。 博学者之怒:2300金币,获得240法术攻击,唯一被动:法术攻击提高30%. 例:如果Sheep_Li买了一个贤者之书和一个博学者之怒,那么她将获得(400+240)*130%=832. 其中,博学者之怒Sheep_Li只会购买一次(唯一被动),贤者之书无限购买。 假设没有购买装备数量限制,Sheep_Li最高可以获得多少法术攻击? 输入描述: 第1行输入测试用例数量T(1<=T<=105) 第2-T+1行输入一个整数N(1<=109),代表初始金币数量。 输出描述: 对于每组测试用例,输出最高法术攻击. 示例1 输入 复制 4 2200 2500 6000 10000 输出 复制 0 312 832 1352 说明 对于用例3,Sheep_Li买了博学者之怒和一个贤者之书. 对于用例4,Sheep_Li买了博学者之怒和两个贤者之书.
时间: 2024-03-30 21:37:31 浏览: 62
这道题可以使用贪心算法来解决。因为贤者之书的攻击力比博学者之怒的攻击力高,所以我们优先选择买贤者之书。当我们买不起贤者之书时,再考虑是否购买博学者之怒。
具体的策略是,如果当前还有足够的金币可以购买贤者之书,则一直购买贤者之书,直到无法再购买为止。然后再判断是否还有足够的金币可以购买博学者之怒,如果可以购买,则购买一次。
下面是参考代码:
相关问题
链接:https://ac.nowcoder.com/acm/contest/59840/D 来源:牛客网 Sheep_Li在打完训练赛后总是喜欢开一把王者,她最近练上了一个新英雄:不知火舞。为了提高二技能扇子的伤害,后期她会把所有其他装备卖掉换成贤者之书或者博学者之怒。 现在问题来了:假设当前她有N枚金币,她需要用这些金币去购买下面两种装备: 贤者之书:2990金币,获得400法术攻击。 博学者之怒:2300金币,获得240法术攻击,唯一被动:法术攻击提高30%. 例:如果Sheep_Li买了一个贤者之书和一个博学者之怒,那么她将获得(400+240)*130%=832. 其中,博学者之怒Sheep_Li只会购买一次(唯一被动),贤者之书无限购买。 假设没有购买装备数量限制,Sheep_Li最高可以获得多少法术攻击?
这是一道贪心问题。我们先尽可能购买博学者之怒,因为它的唯一被动可以让我们获得更多的法术攻击。当金币不足以购买博学者之怒时,我们再购买贤者之书。因为贤者之书可以无限购买,所以我们可以一直购买直到金币不足为止。
具体实现上,我们可以先购买一个博学者之怒,然后不断判断是否还有足够的金币去购买另一个博学者之怒。如果可以,就购买;如果不行了,就购买贤者之书直到金币不足。
以下是Python代码实现:
```python
n = int(input())
ans = 0
while n >= 2300:
ans += 552
n -= 2300
if n >= 2990:
n -= 2990
ans += 400
print(ans)
```
输入金币数量,然后按照上述算法求解并输出结果。
用Java写一个狼和羊在地图上的
以下是用 Java 写的狼和羊在地图上的示例代码:
```
import java.util.Random;
public class Map {
private int width;
private int height;
private boolean[][] occupied;
public Map(int width, int height) {
this.width = width;
this.height = height;
this.occupied = new boolean[width][height];
}
public boolean isOccupied(int x, int y) {
if (x < 0 || x >= this.width || y < 0 || y >= this.height) {
return true;
}
return this.occupied[x][y];
}
public void occupy(int x, int y) {
if (x >= 0 && x < this.width && y >= 0 && y < this.height) {
this.occupied[x][y] = true;
}
}
public void vacate(int x, int y) {
if (x >= 0 && x < this.width && y >= 0 && y < this.height) {
this.occupied[x][y] = false;
}
}
}
public class Animal {
protected int x;
protected int y;
public Animal(int x, int y) {
this.x = x;
this.y = y;
}
public void move(int dx, int dy) {
this.x += dx;
this.y += dy;
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
}
public class Wolf extends Animal {
private int energy;
public Wolf(int x, int y) {
super(x, y);
this.energy = 10;
}
public void eat(Sheep sheep) {
this.energy += sheep.getNutritionValue();
}
public boolean isStarving() {
return this.energy <= 0;
}
public void loseEnergy() {
this.energy--;
}
public int getEnergy() {
return this.energy;
}
}
public class Sheep extends Animal {
private int nutritionValue;
public Sheep(int x, int y) {
super(x, y);
this.nutritionValue = 5;
}
public int getNutritionValue() {
return this.nutritionValue;
}
}
public class Simulation {
private Map map;
private Wolf wolf;
private Sheep sheep;
private Random random;
public Simulation(int width, int height) {
this.map = new Map(width, height);
this.random = new Random();
int wolfX = this.random.nextInt(width);
int wolfY = this.random.nextInt(height);
this.wolf = new Wolf(wolfX, wolfY);
this.map.occupy(wolfX, wolfY);
int sheepX = this.random.nextInt(width);
int sheepY = this.random.nextInt(height);
while (this.map.isOccupied(sheepX, sheepY)) {
sheepX = this.random.nextInt(width);
sheepY = this.random.nextInt(height);
}
this.sheep = new Sheep(sheepX, sheepY);
this.map.occupy(sheepX, sheepY);
}
public void run() {
int time = 0;
while (!this.wolf.isStarving()) {
time++;
System.out.println("Time: " + time);
System.out.println("Wolf: (" + this.wolf.getX() + ", " + this.wolf.getY() + "), Energy: " + this.wolf.getEnergy());
System.out.println("Sheep: (" + this.sheep.getX() + ", " + this.sheep.getY() + ")");
int dx = this.random.nextInt(3) - 1;
int dy = this.random.nextInt(3) - 1;
this.wolf.move(dx, dy);
if (this.wolf.getX() == this.sheep.getX() && this.wolf.getY() == this.sheep.getY()) {
this.wolf.eat(this.sheep);
this.map.vacate(this.sheep.getX(), this.sheep.getY());
int newX = this.random.nextInt(this.map.width);
int newY = this.random.nextInt(this.map.height);
while (this.map.isOccupied(newX, newY)) {
newX = this.random.nextInt(this.map.width);
newY = this.random.nextInt(this.map.height);
}
this.sheep = new Sheep(newX, newY);
this.map.occupy(newX, newY);
}
else {
this.wolf.loseEnergy();
}
System.out.println("=============================================");
}
System.out.println("Wolf is starving!");
}
}
public class Main {
public static void main(String[] args) {
Simulation simulation = new Simulation(10, 10);
simulation.run();
}
}
```
以上代码定义了五个类,`Map`、`Animal`、`Wolf`、`Sheep` 和 `Simulation`。`Map` 类代表地图,有 `width`、`height` 和 `occupied` 三个属性,分别代表宽度、高度和哪些位置被占用了;`Animal` 类代表动物,有 `x` 和 `y` 两个属性,分别代表横坐标和纵坐标;`Wolf` 类继承自 `Animal` 类,有 `energy` 属性,代表能量,还有 `eat`、`isStarving`、`loseEnergy` 和 `getEnergy` 方法,分别代表吃、是否饥饿、消耗能量和获取当前能量值;`Sheep` 类继承自 `Animal` 类,有 `nutritionValue` 属性,代表营养价值,还有 `getNutritionValue` 方法,返回营养价值;`Simulation` 类代表仿真,有 `map`、`wolf`、`sheep` 和 `random` 四个属性,分别代表地图、狼、羊和随机数生成器,还有 `run` 方法,代表运行仿真。
在 `Simulation` 类的构造函数中,会初始化地图、狼和羊。在 `run` 方法中,会循环执行以下操作:
1. 输出当前时间、狼的位置和能量、羊的位置。
2. 随机生成一个移动的方向,并让狼移动。
3. 如果狼和羊在同一个位置,狼吃掉羊,并在地图上移除羊,随机生成一个新的羊,并在地图上放置。
4. 否则,狼消耗能量。
5. 输出分割线。
如果狼的能量小于等于 0,就输出 `Wolf is starving!`。
这只是一个简单的示例,可以根据需要进一步扩展。