您的位置:首页 > 其它

Tempus et mobilius Time and motion栈与队列

2015-08-11 21:30 316 查看
Tempus et mobilius Time and motion

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 828 Accepted: 496
Description


Tempus est mensura motus rerum mobilium.

Time is the measure of movement.--Auctoritates Aristotelis

...and movement has long been used to measure time. For example, the ball clock is a simple device which keeps track of the passing minutes by moving ball-bearings. Each minute, a rotating arm removes a ball bearing from the queue at the bottom, raises it to
the top of the clock and deposits it on a track leading to indicators displaying minutes, five-minutes and hours. These indicators display the time between 1:00 and 12:59, but without 'a.m.' or 'p.m.' indicators. Thus 2 balls in the minute indicator, 6 balls
in the five-minute indicator and 5 balls in the hour indicator displays the time 5:32.

Unfortunately, most commercially available ball clocks do not incorporate a date indication, although this would be simple to do with the addition of further carry and indicator tracks. However, all is not lost! As the balls migrate through the mechanism of
the clock, they change their relative ordering in a predictable way. Careful study of these orderings will therefore yield the time elapsed since the clock had some specific ordering. The length of time which can be measured is limited because the orderings
of the balls eventually begin to repeat. Your program must compute the time before repetition, which varies according to the total number of balls present.

Operation of the Ball Clock

Every minute, the least recently used ball is removed from the queue of balls at the bottom of the clock, elevated, then deposited on the minute indicator track, which is able to hold four balls. When a fifth ball rolls on to the minute indicator track, its
weight causes the track to tilt. The four balls already on the track run back down to join the queue of balls waiting at the bottom in reverse order of their original addition to the minutes track. The fifth ball, which caused the tilt, rolls on down to the
five-minute indicator track. This track holds eleven balls. The twelfth ball carried over from the minutes causes the five-minute track to tilt, returning the eleven balls to the queue, again in reverse order of their addition. The twelfth ball rolls down
to the hour indicator. The hour indicator also holds eleven balls, but has one extra fixed ball which is always present so that counting the balls in the hour indicator will yield an hour in the range one to twelve. The twelfth ball carried over from the five-minute
indicator causes the hour indicator to tilt, returning the eleven free balls to the queue, in reverse order, before the twelfth ball itself also returns to the queue.

Input
The input defines a succession of ball clocks. Each clock operates as described above. The clocks differ only in the number of balls present in the queue at one o'clock when all the clocks start. This number is given for each clock,
one per line and does not include the fixed ball on the hours indicator. Valid numbers are in the range 27 to 127. A zero signifies the end of input.

Output
Output For each clock described in the input, your program should report the number of balls given in the input and the number of days (24-hour periods) which elapse before the clock returns to its initial ordering.

Sample Input
30
45
0

Sample Output
30 balls cycle after 15 days.
45 balls cycle after 378 days.

Source
World Finals 1995
 

题意:
    有一个球类时钟装置,该球钟是一个利用球的移动来记录时间的简单装置.球钟包含了三个可以容纳若干个球的指示器:分钟指示器,五分钟指示器,小时指示器。若分钟指示器中有2个球,五分钟指示器中有6个球,小时指示器中有5个球,则时间为5:32。球钟的工作原理如下:分钟指示器最多可容纳4个球。每过一分钟,球钟就会从球队列的队首滚落下一个球进入分钟指示器,当放入第五个球时,在分钟指示器的4个球就会按照他们被放入指示器时的相反顺序加入球队列的队尾。而第五个球则会滚落入五分钟指示器。按此类推,五分钟指示器最多可放11个球,小时指示器最多可放11个球。当小时指示器放入第12个球时,原来的11个球按照他们被放入时的相反顺序加入球队列的队尾,然后第12个球也回到队尾。此时,三个指示器均为空,回到初始状态,从而形成一个循环。因此一个循环可以表示0-12小时。输入球队列的求数目,球钟的三个指示器初态均为空。问要经过多少天,球钟的球队列才能回复原来的顺序。
解题思路:先把一天情况算出来,然后记录每个元素回到最初位置时间,通过求它们的之间最小公倍数得出最少时间
#include<stdio.h>
#include<queue>
#include<stack>
#include<string.h>
using namespace std;
int vis[200];
int mark[200];
int gcb(int a,int b)
{
return b==0?a:gcb(b,a%b);
}
int lcm(int a,int b)
{
return a*b/gcb(a,b);
}
void fun(int n)
{
stack<int>m1,m5,h;
queue<int>q;
int b,i,j;
for(i=1;i<=n;i++)
{
q.push(i);
}
for(j=1;j<=1440;j++)
{
b=q.front();
q.pop();
if(m1.size()==4)
{
for(i=1;i<=4;i++)
{
q.push(m1.top());
m1.pop();
}
if(m5.size()==11)
{
for(i=1;i<=11;i++)
{
q.push(m5.top());
m5.pop();
}
if(h.size()==11)
{
for(i=1;i<=11;i++)
{
q.push(h.top());
h.pop();
}
q.push(b);
}else h.push(b);
}else m5.push(b);
}
else m1.push(b);
}
for(i=1;i<=n;i++)
{
vis[i]=q.front();
q.pop();
}
}
int main()
{
int n,i,ans;
while(scanf("%d",&n),n)
{
memset(vis,0,sizeof(vis));
memset(mark,0,sizeof(mark));
fun(n);
for(i=1;i<=n;i++)
{
mark[i]=1;
int p=vis[i];
while(p!=i)
{
mark[i]++;//记录每个元素回到最初位置时间
p=vis[p];
}
}
ans=1;
for(i=1;i<=n;i++)
{
ans=lcm(ans,mark[i]);
}
printf("%d balls cycle after %d days.\n",n,ans);
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: