UVA 10935 约瑟夫环
来源:互联网 发布:阿里云上传自己镜像 编辑:程序博客网 时间:2024/05/21 07:46
Throwing cards away I
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Submit
Status
Description
Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is performed as long as there are at least two cards in the deck:
Throw away the top card and move the card that is now on the top of the deck to the bottom of the deck.
Your task is to find the sequence of discarded cards and the last, remaining card.
Each line of input (except the last) contains a number n ≤ 50. The last line contains 0 and this line should not be processed. For each number from the input produce two lines of output. The first line presents the sequence of discarded cards, the second line reports the last remaining card. No line will have leading or trailing spaces. See the sample for the expected format.
Sample input
7
19
10
6
0
Output for sample input
Discarded cards: 1, 3, 5, 7, 4, 2
Remaining card: 6
Discarded cards: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 4, 8, 12, 16, 2, 10, 18, 14
Remaining card: 6
Discarded cards: 1, 3, 5, 7, 9, 2, 6, 10, 8
Remaining card: 4
Discarded cards: 1, 3, 5, 2, 6
Remaining card: 4
Folklore, adapted by Piotr Rudnicki
<span style="color:#6600cc;">/************************************************ author : Grant Yuan time : 2014.8.4 algorithm : 约瑟夫环 source ; UVA 10935************************************************/#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<queue>#include<algorithm>using namespace std;queue<int> q;int n;int main(){ while(1){ scanf("%d",&n); if(n==0) break; while(!q.empty()) q.pop(); for(int i=1;i<=n;i++) { q.push(i); } int t; printf("Discarded cards:"); while(q.size()>1){ if(q.size()==n) printf(" %d",q.front()); else printf(", %d",q.front()); q.pop(); t=q.front(); q.pop(); q.push(t); } cout<<endl; printf("Remaining card: %d\n",q.front());} return 0;}</span>
- UVA 10935 约瑟夫环
- UVa 133 双向约瑟夫环
- uva 10940 (约瑟夫环)
- uva 1452 dp 约瑟夫环
- UVA 1452Jump 【约瑟夫环】
- 约瑟夫环问题 uva 133
- Joseph - acm.uva.305(约瑟夫环)
- UVa 1452 - Jump(约瑟夫环变形)
- uva 1452 - Jump(约瑟夫环)
- UVA 1452-Jump(约瑟夫环问题变型)
- 数学小技巧 UVA 1394 约瑟夫环
- UVA 1452 Jump(约瑟夫环变形)
- UVA 133 The Dole Queue 【约瑟夫环】
- UVa-133 The Dole Queue(约瑟夫环)
- uva 1452(约瑟夫变形)
- uva 133 The Dole Queue 双向约瑟夫环 模拟实现
- uva 305 Joseph(约瑟夫环解+打表)
- UVA 305 Joseph (约瑟夫环 打表)
- VIBE运动检测算法实现(二)
- 一个MFC的MENU类分析(一)
- hdu1421-搬寝室 (经典dp)
- hdu 2243 ac自动机 dp +矩阵快速幂
- CString 按想法把字符串分为2半
- UVA 10935 约瑟夫环
- linux远程copy
- Android调用系统安装
- 友盟在线参数+自动更新升级SDK实现指定版本强制更新升级
- oracle服务器和客户端字符集的查看和修改 .
- mysql_num_rows()函数
- Matlab中find函数用法
- android之【ArrayAdapter】
- synchronized和Lock的异同