Tempus et mobilius Time and motion
来源:互联网 发布:linux dd命令拷贝硬盘 编辑:程序博客网 时间:2024/06/05 07:09
Tempus et mobilius Time and motion
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 826 Accepted: 494
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
30450
Sample Output
30 balls cycle after 15 days.45 balls cycle after 378 days./*题意: 有一个球类时钟装置,该球钟是一个利用球的移动来记录时间的简单装置.球钟包含了三个可以容纳若干个球的指示器:分钟指示器,五分钟指示器,小时指示器。若分钟指示器中有2个球,五分钟指示器中有6个球,小时指示器中有5个球,则时间为5:32。球钟的工作原理如下:分钟指示器最多可容纳4个球。每过一分钟,球钟就会从球队列的队首滚落下一个球进入分钟指示器,当放入第五个球时,在分钟指示器的4个球就会按照他们被放入指示器时的相反顺序加入球队列的队尾。而第五个球则会滚落入五分钟指示器。按此类推,五分钟指示器最多可放11个球,小时指示器最多可放11个球。当小时指示器放入第12个球时,原来的11个球按照他们被放入时的相反顺序加入球队列的队尾,然后第12个球也回到队尾。此时,三个指示器均为空,回到初始状态,从而形成一个循环。因此一个循环可以表示0-12小时。输入球队列的求数目,球钟的三个指示器初态均为空。问要经过多少天,球钟的球队列才能回复原来的顺序。算法分析: 理解题意之后首先想到直接用队列和栈进行模拟。由于模拟复杂度较高,算法效率不高,想到置换群中所讲到的方法,从一个状态置换再次回到原来状态的顺序所需的次数是所有循环节长度的最小公倍数。从而将题目简化成一个简单的queue + stack的入队出队模拟和最小公倍数问题!*/#include<stdio.h>#include<string.h>#include<math.h>#include<stack>#include<queue>#include<algorithm>using namespace std;int mem[7100],mark[7100];int gcd(int a,int b){return b==0?a:gcd(b,a%b);}int lcm(int a,int b){return a*b/gcd(a,b);}void sss(int n){queue<int>q;stack<int>mins,f_mins,hours;///分别表示分钟指示器,5分钟指示器,时钟指示器for(int i=0;i<n;i++)///将n入队,形成球队列q.push(i);for(int t=0;t<1440;t++)///对24小时,1440分钟进行枚举{int num=q.front();q.pop();if(mins.size()==4)///分钟指示器中已存在4个球{for(int i=0;i<4;i++){q.push(mins.top());///将分钟指示器中的球按相反顺序放入球队列队尾mins.pop(); }if(f_mins.size()==11)///5分钟指示器中已存在11个球 {for(int i=0;i<11;i++){q.push(f_mins.top());///将5分钟指示器中的球按相反顺序放入球队列队尾f_mins.pop();}if(hours.size()==11)///时钟指示器中已存在11个球{for(int i=0;i<11;i++){q.push(hours.top());///将时钟指示器中的球按相反顺序放入球队列队尾hours.pop(); }q.push(num);///时钟指示器中的球等于12,则将入指示器的球放入队尾 }elsehours.push(num); }elsef_mins.push(num);} elsemins.push(num); } for(int j=0;j<n;j++)///存储球队列的状态{mem[j]=q.front();q.pop();}}int main(){int n;while(scanf("%d",&n),n){memset(mem,0,sizeof(mem));memset(mark,0,sizeof(mark));sss(n);int ans=1;for(int i=0;i<n;i++)///计算出循环节长度cnt,并求最小公倍数{if(!mark[i]){mark[i]=1;int j=mem[i];int cnt=1;while(!mark[j]){mark[j]=1;cnt++;j=mem[j];}ans=lcm(ans,cnt);}}printf("%d balls cycle after %d days.\n",n,ans);}return 0;}/*另一种方法*/
#include <iostream>#include <cstdio>#include <vector>#include <string.h>#define maxn 1005#include<algorithm>using namespace std;int N,M;int a[200];int q[60*24*10];int Mstack[3][20];//sec min houint top[3];int vis[200];int head,tail;int gcd(int a,int b){return b==0?a:gcd(b,a%b);}int solve(){int i,j,k,flag;int cnt,ans;for(i=1;i<=N;i++)q[i]=a[i]=i;head=1;tail=N+1;memset(top,0,sizeof(top));memset(vis,0,sizeof(vis));for(j=tail,i=1;i<=60*24;i++){if(top[0]==4){for(k=0;k<4;k++) q[j++]=Mstack[0][--top[0]];if(top[1]==11){for(k=0;k<11;k++) q[j++]=Mstack[1][--top[1]];if(top[2]==11){for(k=0;k<11;k++) q[j++]=Mstack[2][--top[2]];q[j++]=q[i];}elseMstack[2][top[2]++]=q[i];}elseMstack[1][top[1]++]=q[i];}elseMstack[0][top[0]++]=q[i];}ans=1;for(j=i;j<N+i;j++){if(vis[j-i+1]==0){vis[j-i+1]=1;k=q[j];cnt=1;while(vis[k]==0){cnt++;vis[k]=1;k=q[i+k-1];}ans=ans/gcd(ans,cnt)*cnt;}}return ans;}int main(){ while(scanf("%d",&N),N) {printf("%d balls cycle after %d days.\n",N,solve());} return 0;}
0 0
- Tempus et mobilius Time and motion
- poj 1879.Tempus et mobilius Time and motion
- poj 1879 Tempus et mobilius Time and motion
- UVA 239 - Tempus et mobilius. Time and motion(置换周期)
- uva 239 - Tempus et mobilius. Time and motion(置换)
- POj 1879 Tempus et mobilius Time and motion (模拟+群)
- POJ 1879 - Tempus et mobilius Time and motion【置换群】
- Tempus et mobilius Time and motion栈与队列
- poj 1879 Tempus et mobilius Time and motion
- POJ1879 Tempus et mobilius Time and motion【置换群】
- POJ 1879 Tempus et mobilius Time and motion(黑书,小球钟)
- POJ 1879 Tempus et mobilius Time and motion 队列和栈
- JOJ 1060: Time And Motion 解题报告
- Real-Time Video Super-Resolution with Spatio-Temporal Networks and Motion Compensation 论文笔记
- Fire and Motion
- CVPR2016 Motion and Tracking
- Fire And Motion(英文原版) [转]
- CALCULATE GLOBAL MOTION AND SMOOTHNESS
- cadence 安装使用问题
- byte数组与字符串转化类(php)
- Gradle多渠道打包
- 单片机流水灯
- I学霸官方免费教程二十:Java常用类之StringBuffer类和StringBuilder类
- Tempus et mobilius Time and motion
- shell中单引号,双引号,反引号和反斜杠的区别
- Swift学习笔记系列——(20)扩展
- ios NavigationViewController跳转以及返回传值
- shell变量(字符串)间的连接
- j-hi项目及应用说明
- DirectShow学习笔记总结
- MFC中如何将焦点设置到指定控件上
- linux安装gcc-arm-none-eabi