栈和队列的综合应用,利用栈和队列分别模拟分,5分,时槽,以及小球队列

利用求出一天后的置换可以求出周期,进而求出最大公约数(可以利用矩阵的角度,也许可以简化,因为每次都是乘上一个相同的置换矩阵)

要注意读题,时槽 满12归队的方式很不一样

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std; const int maxn= 127+5;
const int dms= 24*60; int T[maxn], chg[maxn];
stack<int> h, fvm, m;
queue<int> bl;
int ans; int Gcd(int a, int b)
{
return 0== b? a : Gcd(b, a%b);
}
void Init(int n)
{
ans= 1;
while (!bl.empty()){
bl.pop();
}
while (!h.empty()){
h.pop();
}
while (!fvm.empty()){
fvm.pop();
}
while (!m.empty()){
m.pop();
} for (int i= 0; i< n; ++i){
T[i]= 1;
}
for (int i= 0; i< n; ++i){
bl.push(i);
}
}
int main()
{
int n;
while (1){
scanf("%d", &n);
if (0== n){
break;
}
Init(n); for (int i= 0; i< dms; ++i){
int obl= bl.front();
bl.pop();
if (4== m.size()){
for (int j= 0; j< 4; ++j){
bl.push(m.top());
m.pop();
}
if (11== fvm.size()){
for (int j= 0; j< 11; ++j){
bl.push(fvm.top());
fvm.pop();
}
if (11== h.size()){
for (int j= 0; j< 11; ++j){
bl.push(h.top());
h.pop();
}
bl.push(obl);
}else{
h.push(obl);
}
}
else{
fvm.push(obl);
}
}
else{
m.push(obl);
}
} for (int i= 0; i< n; ++i){
chg[i]= bl.front();
// cout<<i<<" "<<chg[i]<<endl;
bl.pop();
} for (int i= 0; i< n; ++i){
int ni= chg[i];
while (ni!= i){
ni= chg[ni];
++T[i];
}
} for (int i= 0; i< n; ++i){
ans= ans*T[i]/Gcd(ans, T[i]);
}
cout << n << " balls cycle after "<< ans<<" days."<<endl;
} return 0;
}

最新文章

  1. DataFrame格式化
  2. Unable to find messages file &#39;cscui.dll&#39; 问题解决
  3. 去除angularjs路由的显眼的#号
  4. 更改printk打印级别
  5. solr5.5教程-solr.home 配置
  6. 【转】Android -- Looper.prepare()和Looper.loop()
  7. opencv 手势识别
  8. 类型&lt;T&gt; where T:class的用法
  9. 《分布式Java应用之基础与实践》读书笔记二
  10. redis启动脚本报错
  11. linux 巨页使用测试
  12. C#,DataHelper,一个通用的帮助类,留个备份。
  13. 给定整数a1、a2、a3、...、an,判断是否可以从中选出若干个数,使得它们的和等于k(k任意给定,且满足-10^8 &lt;= k &lt;= 10^8)。
  14. django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3.
  15. Iterator接口(迭代器)
  16. WebApi 运行原理
  17. insserv: Script &lt;name&gt; is broken: incomplete LSB comment.
  18. dart --- 更符合程序员编程习惯的javascript替代者
  19. pythoon 学习资源
  20. Android-Bundle认知、和Intent的差别

热门文章

  1. 关于虚拟机VMware Tools安装中出现的无法自动安装VMCI驱动程序的问题
  2. highcharts 柱状图在柱子顶部显示y轴数据
  3. 修改npm安装的全局路径和配置环境变量
  4. 翻译:谷歌HTML、CSS和JavaScript风格规范
  5. Django学习之路03
  6. 在C代码调用C++代码
  7. vmware增加新硬盘无需重启生效
  8. objectarx 得到有宽度的多段的轮廓
  9. 改进&quot;尽最大努力交付&quot;的服务
  10. C#使用正则表达式获取HTML代码中a标签里包含指定后缀的href的值