题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5417
题目大意:有一台机器,启动的瞬间会弹出一个小球,之后每过w秒弹出一个小球,但这个机器有个缺陷,每启动x秒之后会关闭(关闭的瞬间也可能会有小球弹出)y秒,之后再次重启。找出第n个小球弹出的时间。
分析:有小球弹出无非下列几种情况:(1)机器启动;(2)启动w秒;对于第一种情况,我们直接计数即可,对于第二种情况,我们只需找出在x秒内有多少个w即可。
实现代码如下:
#include <cstdio>
#include <iostream>
using namespace std;
int main()
{
int x,y,w,n;
while(scanf("%d%d%d%d",&x,&y,&w,&n)!=-1)
{
if(n==1)
{
puts("0");
continue;
}
int time=0;
int num=1;
while(num<n)
{
int cnt=0;
for(int i=1;i<=x;i++)
{
if(cnt+w<=x)
{
cnt+=w;
num++;
}
if(num==n) break;
}
time+=cnt;
if(num==n) break;
time=time+(x-cnt)+y;
num++;
}
printf("%d\n",time);
}
return 0;
}