UVa 133 The Dole Queue
2015-09-24 14:49
411 查看
解题思路:约瑟夫环。具体见代码:
View Code
#include<cstdio> using namespace std; int n, k, m, vis[24]; //24,向科比致敬,像科比一样努力的活着。 int go(int p, int d, int t) { while(t--) { do{ p = (p+d+n-1)%n + 1; //在纸上写写就知道 }while(vis[p]==0); //如果是0,则继续走,并且不计入步数。 } return p; //返回走到的位置 } int main() { while(~scanf("%d%d%d", &n, &k, &m) && n) { for(int i = 1; i <= n; i++) vis[i] = i;//初始化 int left = n, p1 = n, p2 = 1; //这里的初始化要注意 while(left) { p1 = go(p1, 1, k); //1表示逆时针 p2 = go(p2, -1, m); //-1表示顺时针 printf("%3d", vis[p1]); left --; if(p2 != p1) //如果不同才输出 { printf("%3d", vis[p2]); left --; } vis[p1] = vis[p2] = 0; //标记为0,表示已走过 if(left) printf(","); //最后一个没有标点 } printf("\n"); } return 0; }
View Code
相关文章推荐
- Worst Performing Queries
- UIProgressView进度条方法总结
- 网页性能管理详解:浅谈chrome-Timeline及window.requestAnimationFrame()方法
- OpenStack: Perform Consistent Snapshots With Qemu Guest Agent
- 字符串0.在php和js中转换为布尔类型 值是false还是true
- iOS之UITableView
- setRequestedOrientation设置屏幕方向
- 如何使用xib自定义UITableViewCell
- Android UI设计
- 重写UITabBar 关键步骤小结
- 14-EMM Procedure 4. Service Request
- Android 异步查询框架AsyncQueryHandler的使用
- Number Sequence---hdu1711(kmp)
- UILabel UiButton 文字下面加下划线
- UILabel,UITextField 以及UIButton应用
- Liferay中request
- leetcode 95-96:Unique Binary Search Trees
- <s:checkbox> fieldValue取值问题
- Introduction to Java Programming编程题8.37<Guess the capitals>
- White spaces are required between publicId and systemId