您希望从著名的在线视频游戏商店Mist购买视频游戏。
通常,所有游戏都以相同的价格出售,即美元。然而,他们计划在下个月进行季节性的万圣节大减价,你可以用更便宜的价格购买游戏。具体来说,您在促销期间购买的第一款游戏将以美元出售,但您随后购买的每一款游戏的销售价格都将比您之前购买的游戏的成本低。这将继续下去,直到成本变得小于或等于美元,在此之后,您购买的每个游戏都将花费美元。
例如,如果和,则以下是您购买的第一个游戏的成本,按顺序排列:
你的迷雾钱包里有美元。在万圣节大减价期间你能买多少游戏?
我的代码是这样的,我想知道为什么11/52测试用例会失败。
#include<stdio.h>
int main()
{
int ct=0,i,p,d,m,s;
scanf("%d %d %d %d",&p,&d,&m,&s);
i=p;
while(i<s)
{
if(p-d>m) p-=d;
else p=m;
i+=p;
ct+=1;
}
printf("%d",ct);
return 0;
}发布于 2020-11-12 20:31:54
只要你还有剩余的钱,你就可以增加ct,但是如果你剩下的钱不足以支付游戏的成本,那么你就不能买它。
发布于 2020-11-13 04:35:59
@Dialecticus是对的。您的问题缺少关键信息。虽然我已经解决了同样的Hackerrank problem,并通过了所有的测试用例。我只会分享伪代码,因为我的解决方案是JS,而不是C++。
counter = 0; // sales counter
// we still have enough money for a normal and a discounted game
while s >= m && s >= p
// Apply discount after one game
if counter > 0
p = the larger price between the discounted price & min price
// deduct price from wallet
s -= p;
// record the sale
counter++
return counter我决定包含实际的JS代码,也许您可以转换它。
let counter = 0;
while(s >= m && s >= p) {
p = counter > 0 ? Math.max(p - d, m) : p;
s -= p;
counter++;
}
return counter;发布于 2020-11-13 11:01:43
为什么不使用公式呢?类似于:
if (p > s)
return 0;
int t = min(
ceil((p - m) / (float)d),
floor((sqrt(8 * ceil((s - p) / (float)d) + 1) - 1) / 2)
);
return t + floor((s - (p * t - d * t * (t - 1) / 2)) / m);https://stackoverflow.com/questions/64803879
复制相似问题