- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
你和 100 个对手玩游戏。游戏有 k 轮。每一轮你都可以消灭一些对手(总是至少 1 个)。您将因消除它们而获得奖励。
奖励为:100.000 * '被淘汰的对手数'/'对手数' <= 整数(向下舍入)
我想以某种方式消灭对手,让我获得尽可能多的钱。
示例游戏:
我尝试写一些东西,但我认为这不是最有效的方法?
Program EliminationGame;
var
selectedHistory : array [1..10] of integer;
opponentCount,roundCount : integer;
maxOpponents,numberSelected : integer;
totalMoney : integer;
i : integer;
begin
totalMoney := 0;
maxOpponents := 100;
opponentCount := maxOpponents;
roundCount := 3; {test value}
for i:=1 to roundCount do begin
if (i = roundCount) then begin
numberSelected := opponentCount;
end else begin
numberSelected := floor(opponentCount / roundCount);
end;
selectedHistory[i] := numberSelected;
totalMoney := floor(totalMoney + (numberSelected / opponentCount * 100000));
opponentCount := opponentCount - numberSelected;
end;
writeln('Total money won:');
writeln(totalMoney);
writeln('Amount selected in rounds:');
for i:= 0 to Length(selectedHistory) do
write(selectedHistory[i],' ');
end.
另外似乎pascal中不存在floor函数?
最佳答案
看来这个问题有一个可以提前计算的数学答案。正如@Anton 所说,很明显,第三轮给出的分数与消灭敌人的数量无关。所以第三回合应该消灭1个敌人。
所以我们得到了一个三轮游戏的函数。
f(x)=100000x/100+100000(99-x)/(100-x)+100000*1/1, where x- the number of enemies eleminated at first round.
如果我们找到极值(函数的局部最大值),它看起来等于 90。这意味着决定如下:第一轮消除 90,第二轮 - 9,第三轮 - 1 个敌人。当然,考虑一下:90=100-sqrt(100)。
换句话说:任务的Pascal决策是循环一个从1到99的变量,看这个函数的最大值。 X-将是答案。
program Project1;
var
x, xmax: byte;
MaxRes, tmp: real;
begin
xmax := 0;
MaxRes := 0;
for x := 1 to 99 do
begin
tmp := 100000 * x / 100 + 100000*(99 - x) / (100 - x) + 100000 * 1 / 1;
if tmp > MaxRes then
begin
MaxRes := tmp;
xmax := x;
end;
end;
writeln(xmax);
readln;
end.
一般决定其他数量的敌人和回合(使用递归)如下(Delphi 方言):
program Project1;
{$APPTYPE CONSOLE}
{$R *.res}
Uses System.SysUtils;
var
s: string;
function Part(RemainingEnemies: byte; Depth: byte;
var OutputString: string): real;
var
i: byte;
tmp, MaxRes: real;
imax: byte;
DaughterString: string;
begin
OutputString := '';
if Depth = 0 then
exit(0);
imax := 0;
MaxRes := 0;
for i := 1 to RemainingEnemies - Depth + 1 do
begin
tmp := i / RemainingEnemies * 100000 + Part(RemainingEnemies - i, Depth - 1,
DaughterString);
if tmp > MaxRes then
begin
MaxRes := tmp;
imax := i;
OutputString := inttostr(imax) + ' ' + DaughterString;
end;
end;
result := MaxRes;
end;
begin
writeln(Part(100, 3, s):10:1);//first parameter-Enemies count,
//2-Number of rounds,
//3-output for eliminated enemies counter
writeln(s);
readln;
end.
关于algorithm - 一场有100个对手的比赛,赢尽可能多的钱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34254245/
使用掷骰子游戏中的 10,000 次模拟来计算获胜概率 (wins/(wins + Loss))。下面是双骰子游戏的方法: public class CrapsGame { public st
我想制作一个程序作为检查网,如果地址向上,则返回 1;如果地址向下,则返回 0。代码如下: #include #include #include #pragma comment(lib, "ws
我正在尝试使用 Win API ReadConsole(...),我想传入一个分隔符字符以停止来自控制台的输入。下面的代码有效,但它只会停止读取 \r\n 上的输入。例如,我希望它停止读取 '.' 上
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我想在 d3.js 中制作一个条形图,每个项目或行都有正条和负条,如下所示: 它有点像谷歌金融“行业摘要”图表(http://google.com/finance) 任何人都可以指出我这种图表的 d3
尝试根据 GF 和 GA 创建一个新列“Results”。想知道如何通过定义函数和条件语句来做到这一点。以下是我对一行的初步尝试,但无法弄清楚如何将其应用于所有行,非常感谢您的帮助! 日期地点对手GF
过去,我通过 wpf 使用 mvvmlight 并取得了一定程度的成功。我最近看到了 Caliburn Micro,但我想知道是否有人对此有任何经验。 我看到了 Caliburn Micro 的网站,
我正在尝试将 FFMPEG(我实际上只是在寻找 FFProbe,它包含在 FFMPEG 工具中)编译为适用于 MacOS X、Linux 和可能的 Windows 的独立应用程序。对于独立,我的意思是
你能告诉我你是如何让 PartCover 在 VS2008 上运行并赢得 7 x64 的吗?基于此帖子 How do I run PartCover in x64 windows ,我跑了 c:\Pr
我要抓: 窗口调整大小/移动/最小化/最大化/关闭消息。 鼠标点击和键盘按下。 当用户按 enter 或 dblclick 执行任何程序时。 (如果可能的话?) 这应该与键锁程序的工作方式相同:如果您
我正在创建一个实用软件,其中存储了 PC 的所有详细信息,我设法找到了一个包含我想要的所有详细信息的程序“msinfo32”。 目前的问题是我不知道如何尽快在软件中获取这些信息。 请帮我写一段代码 提
我需要我的一个 .exe 文件在没有 UAC 提示的情况下始终以管理员身份运行。我的程序将与安装程序一起安装,它将有一次管理员权限,我需要在这个安装程序中执行这样的步骤,我的 exe 将始终以管理员身
这个错误发生在前一段时间,2011年 http://cygwin.com/ml/cygwin/2013-11/msg00021.html 根据我在这篇文章和其他一些文章中所读到的内容,我看到他们写了一
关于 tel 协议(protocol)处理程序:除了在窗口 10 下,我的应用程序工作正常。在窗口 10 下需要哪些额外的注册表项才能为 tel 协议(protocol)注册应用程序(因此我的应用程序
我是一名优秀的程序员,十分优秀!