HDU2319 Card Trick
来源:互联网 发布:淘宝公益宝贝是正品吗 编辑:程序博客网 时间:2024/06/03 18:25
Card Trick
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Problem Description
The magician shuffles a small pack of cards, holds it face down and performs the following procedure:
1.The top card is moved to the bottom of the pack. The new top card is dealt face up onto the table. It is the Ace of Spades.
2.Two cards are moved one at a time from the top to the bottom. The next card is dealt face up onto the table. It is the Two of Spades.
3.Three cards are moved one at a time…
4.This goes on until the nth and last card turns out to be the n of Spades.
This impressive trick works if the magician knows how to arrange the cards beforehand (and knows how to give a false shuffle). Your program has to determine the initial order of the cards for a given number of cards, 1 ≤ n ≤ 13.
1.The top card is moved to the bottom of the pack. The new top card is dealt face up onto the table. It is the Ace of Spades.
2.Two cards are moved one at a time from the top to the bottom. The next card is dealt face up onto the table. It is the Two of Spades.
3.Three cards are moved one at a time…
4.This goes on until the nth and last card turns out to be the n of Spades.
This impressive trick works if the magician knows how to arrange the cards beforehand (and knows how to give a false shuffle). Your program has to determine the initial order of the cards for a given number of cards, 1 ≤ n ≤ 13.
Input
On the first line of the input is a single positive integer, telling the number of test cases to follow. Each case consists of one line containing the integer n.
Output
For each test case, output a line with the correct permutation of the values 1 to n, space separated. The first number showing the top card of the pack, etc…
Sample Input
245
Sample Output
2 1 4 33 1 4 5 2
Source
Nordic 2006
用数组模拟来做,就是想的时候有点别扭。
#include<stdio.h>#include<string.h>int main(){ int n,i,l; scanf("%d",&n); int a[1000]; for(i=1;i<=n;i++) { memset(a,0,sizeof(a)); int m; int j,k; int cnt=2; scanf("%d",&m); if(m==1) printf("1\n"); else if(m==2) printf("2 1\n"); else { a[2]=1; j=0; k=2; for(;;) { ++k; if(a[k]==0) j++; if(j==cnt+1) { a[k]=cnt; cnt++; j=0; } if(k==m) k=0; if(cnt==m+1) break; } for(l=1;l<m;l++) printf("%d ",a[l]); printf("%d\n",a[m]); } } return 0;}下面是借鉴别人用队列做的
#include<stdio.h>#include<iostream>#include<queue>using namespace std;queue<int>q;int flag;void output(){ int temp=q.front(); q.pop(); while(!q.empty()) output(); if(flag==0) { printf("%d",temp); flag=1; } else printf(" %d",temp);}int main(){ int n; scanf("%d",&n); while(n--) { int t; scanf("%d",&t); while(t>0) { q.push(t); int cnt=t; t--; while(cnt--) { int temp=q.front(); q.pop(); q.push(temp); } } flag=0; output(); printf("\n"); } return 0;}
0 0
- HDU2319 Card Trick
- Card Trick
- Card Trick
- Card Trick pku 3032
- PKU3032 Card Trick
- poj 3032.Card Trick
- POJ3032 Card Trick
- NYOJ 714 Card Trick
- NYOJ 714 Card Trick
- POJ,3032,Card Trick
- 1018 A Card Trick
- poj 3032 Card Trick
- NYOJ 714 Card Trick
- NYOJ714-Card Trick
- POJ 3032Card Trick
- 2485. Card Trick
- 714 Card Trick
- 1018. A Card Trick
- jQuery 遍历DOM
- java isInterrupted()和interrupted()
- Opengl之transform feedback与粒子系统
- 前台页面Json对象取值问题
- 教你如何一步步将项目部署到Github
- HDU2319 Card Trick
- Vue js 的生命周期(看了就懂)
- pat-a1060. Are They Equal (25)
- 多线程中使用curl致coredump问题
- java:通过Calendar类正确计算两日期之间的间隔
- ROI Pooling层解析
- MySQL数据库引擎
- log4j
- 各种License介绍