HDOJ 5417 Victor and Machine
Problem Description
Victor has a machine. When the machine starts up, it will pop out a ball immediately. After that, the machine will pop out a ball everyw
seconds. However, the machine has some flaws, every time after
x
seconds of process the machine has to turn off for
y
seconds for maintenance work. At the second the machine will be shut down, it may pop out a ball. And while it's off, the machine will pop out no ball before the machine restart.
Now, at the0
second, the machine opens for the first time. Victor wants to know when the
n -th
ball will be popped out. Could you tell him?
Now, at the
Input
The input contains several test cases, at most
100
cases.
Each line has four integersx ,y ,w
and n .
Their meanings are shown above。
Each line has four integers
Output
For each test case, you should output a line contains a number indicates the time when then -th
ball will be popped out.
Sample Input
2 3 3 3 98 76 54 32 10 9 8 100
Sample Output
10 2664 939
题目大意:一个机器每隔w (/s)弹射一颗小球。机器开启的瞬间(第0时刻)会有一颗小球弹出。可是每隔 x (/s)机器会关机,修整y (/s)然后重新启动。
非常明显是一个以 ( x+y) 为周期的题目,求出每一个周期会有几颗小球弹射就能够了,注意边界小球的处理。须要注意的是假设 w > x 那么w将是没实用的条件,并且时间不进行累计。
思路:这样的运动是有周期的,找到单个区间的数量关系即可了,然后在处理一下余数的问题
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #define LL int #define inf 0x3f3f3f3f using namespace std; int main() { LL n,m,x,w,y,i,j,cycle; while(~scanf("%d%d%d%d",&x,&y,&w,&n)) { cycle=x/w+1;//一个周期内小球的弹出数量 printf("%d\n",((n-1)/cycle)*(x+y)+( (n-1)%cycle )*w ); }//求第n个小球的弹出数量=n-1个小球弹出的总时间 return 0; }
或者,枚举n产生的条件:
int X=x; int time=0; for(int i=2;i<=n;i++) { if(time+w<=x) { time+=w; } else { time=x; time+=y; x=x+y+X; } } cout<<time<<endl;