Problem Description
小沃沃在玩一个有趣的游戏。
初始他有 n 块钱,每一轮他需要投入至少 m 块钱,系统会拿走其中 p% 的钱,并把剩下的钱还给他。
请问在最优情况下,小沃沃最多可以玩多少轮?
假设当前一轮小沃沃投入了 x 块钱,那么他可以收回 ⌊x×(1−p%)⌋ 块钱,其中 ⌊a⌋ 表示 a 取下整。
小沃沃每一轮投入的钱不能超过他现在拥有的钱。
每一轮投入的钱必须为整数。
Input
第一行一个正整数 test(1≤test≤100000) 表示数据组数。
对于每组数据,一行三个整数 n,m,p(1≤n≤100000,1≤m≤1000,1≤p≤100)。
Output
对每组数据输出一行一个整数表示答案。
Sample Input
2
10 2 50
10 2 100
Sample Output
9
5
一开始错误的程序
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
int t,n,m,p;
scanf("%d",&t);
while (t--)
{
int ans=0;
scanf("%d%d%d",&n,&m,&p);
int k=ceil((100-p)*0.01*m);
if (k==0) printf("%d\n",n/m);
else
{
while (n>=m)
{
ans++;
n-=k;
}
printf("%d\n",ans);
}
}
return 0;
}
错误原因:
int k=ceil((100-p)*0.01*m); //这是回收的钱……(傻了的我)
while (n>=m) //耗时长,需要改进
{
ans++;
n-=k;
}
AC代码:
#include<cstdio>
#include<cmath>
using namespace std;
int main()
{
int t,n,m,p;
scanf("%d",&t);
while (t--)
{
int ans=0;
scanf("%d%d%d",&n,&m,&p);
int k=ceil(m*p*0.01); //每次拿走钱
while (n>=m)
{
int cnt=n/m; //当不补时候可以玩几轮
ans+=cnt;
n-=cnt*k;
}
printf("%d\n",ans);
}
return 0;
}