uva 133 The Dole Queue 双向约瑟夫环 模拟实现
双向约瑟夫环。
数据规模只有20,模拟掉了。(其实公式我还是不太会推,有空得看看)
值得注意的是两个方向找值不是找到一个去掉一个,而是找到后同时去掉。
还有输出也很坑爹!
在这里不得不抱怨下Uva的oj,少了个s,少了个空行什么的都不会显示pe,就给个wa,让人还以为算法有问题。原本以为Uva没有pe,然后据说这边的输出空行如果不对的话就会显示pe。。。
代码:
#include <cstdio>
#include <cstring>
const int maxn = 22; int N;
bool d[maxn]; int cw(int pos, int k) {
int cnt = 0;
for ( ; cnt != k; pos++) {
if (pos == N) pos = 0;
if (d[pos]) cnt++;
}
return --pos;
} int w(int pos, int m) {
int cnt = 0;
for (; cnt != m; pos--) {
if (pos == -1) pos = N - 1;
if (d[pos]) cnt++;
}
return ++pos;
}
int main() {
int k, m;
while (scanf("%d%d%d", &N, &k, &m) && N && k && m) {
memset(d, 1, N);
int tot = N, flag = 0, a = 0, b = N - 1;
while (tot) {
a = cw(a, k), b = w(b, m);
tot--;
if (flag) printf(",");
d[a] = d[b] = false;
flag = 1;
if (a == b)
printf("%3d", a + 1);
else {
printf("%3d%3d", a + 1, b + 1);
tot--;
}
}//while find out all
printf("\n");
}//while
return 0;
}
uva 133 The Dole Queue 双向约瑟夫环 模拟实现的更多相关文章
- UVA 133 The Dole Queue
The Dole Queue 题解: 这里写一个走多少步,返回位置的函数真的很重要,并且,把顺时针和逆时针写到了一起,也真的很厉害,需要学习 代码: #include<stdio.h> # ...
- uva - 133 The Dole Queue(成环状态下的循环走步方法)
类型:循环走步 #include <iostream> #include <sstream> #include <cstdio> #include <cstr ...
- UVA 133 The Dole Queue(报数问题)
题意:一个长度为N的循环队列,一个人从1号开始逆时针开始数数,第K个出列,一个人从第N个人开始顺时针数数,第M个出列,选到的两个人要同时出列(以不影响另一个人数数),选到同一个人就那个人出列. 思路: ...
- UVA 133“The Dole Queue”(循环报数处理技巧)
•参考资料 [1]:紫书P82 •题意(by紫书) 按照被选中的次序输出这 n 个人的编号: 如果A和B选中的是同一个人,输出一个这个人的编号: 输出格式:输出的每个编号占3个字节,不够3个字节在前面 ...
- uva 133(The Dole Queue UVA - 133)
一道比较难想的模拟题,用了队列等东西,发现还是挺难做的,索性直接看了刘汝佳的代码,发现还是刘汝佳厉害! 代码本身难度并不是很大,主要还是p=(p+n+d-1)%n+1;这一句有些难度,实际上经过自己的 ...
- Roman Roulette(约瑟夫环模拟)
Roman Roulette Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- python中使用queue实现约瑟夫环(约瑟夫问题)求解
约瑟夫问题:是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围. 从编号为k的人开始报数,数到m的那个人出列:他的下一个人又从1开始报数,数到m的那个人又出列: 依 ...
- uvaoj 133 - The Dole Queue(逻辑,环形队列数数)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 10行Python代码解决约瑟夫环(模拟)
http://blog.csdn.net/dengyaolongacmblog/article/details/39208675 #!/usr/bin/env python # coding: utf ...
随机推荐
- Gym 100507J Scarily interesting! (贪心)
Scarily interesting! 题目链接: http://acm.hust.edu.cn/vjudge/contest/126546#problem/D Description This y ...
- mac 下对 iterm 终端 设置代理
vi .profile export http_prox="http://xxxx:port" export https_proxy="http://xxxx:port& ...
- 浅析网站开发中的 meta 标签的作用
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- 如何给word 文章的每段段尾添加 脚注
新建一个宏,其代码如下 Sub 段尾加脚注() ' ' 段尾加脚注 宏 ' 'ActiveDocument.Paragraphs.Count返回文档的段落数 ' 定义一个整形变量numpara,来存储 ...
- php 常用设计模式详解
1.单例模式 构造函数必须为private 一个保存类实例静态成员变量 拥有一个访问这个实例的公共静态方法(常用getInstance()方法进行实例化单例类,通过instanceof操作符可以检测到 ...
- CF:322D - Ciel and Duel 贪心 或者 DP 我用的贪心 。。难道sort跟qsort是不一样的么?
D. Ciel and Duel time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Linux 安装oracle10g 配置dataguard 介绍和步骤
DataGuard是甲骨文推出的一种高可用性数据库方案,在Oracle 8i之前被称为Standby Database.从Oracle 9i开始,正式更名为Data Guard.它是在 ...
- Don't Repeat Yourself (不要重复你自己)
DRY是指Don't Repeat Yourself特指在程序设计以及计算中避免重复代码,因为这样会降低灵活性.简洁性,并且可能导致代码之间的矛盾.<The Pragmatic Programm ...
- synthesize(合成) keyword in IOS
synthesize creates setter and getter (从Objective-C 2.0开始,合成可自动生成存取方法) the setter is used by IOS to s ...
- hdu 5565 Clarke and baton 二分
Clarke and baton Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...