nyoj 714 打牌的技巧(队列)
2016-05-19 21:24
369 查看
Card Trick
时间限制:1000 ms | 内存限制:65535 KB难度:3描述
The magician shuffles a small pack of cards, holds it face down and performs the following procedure:
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.
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.
Three cards are moved one at a time…
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.
输入On the first line of the input is a single positive integer k, telling the number of test cases to follow. 1 ≤ k ≤ 10 Each case consists of one line containing the integer n. 1 ≤ n ≤ 13输出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…样例输入
245
样例输出
2 1 4 33 1 4 5 2
分析:
题意就是第几次就从上面拿几张牌放到下面,然后把当前最上面的牌拿出,直到纸牌全部取出,需要注意的是:题中要输出的是每张牌输出的时间,而不是牌的输出顺序,所以需要另外一个数组存储输出时间;
#include<stdio.h>
#include<queue>
using namespace std;
int main()
{
int k,m,n,a[15],b[15],i,j;
scanf("%d",&k);
while(k--)
{
queue<int>q;
scanf("%d",&m);
for(i=1; i<=m; i++)
q.push(i);//先把所有的数据都放入队列
for(i=1; i<=m; i++)
{
n=i;
while(n--)//把i张牌放在下边,重新开始洗牌
{
int t=q.front();
q.pop();
q.push(t);
}
a[i]=q.front();//把当前最上面的一张存入数组
q.pop();
}
for(i=1; i<=m; i++)//把a数组每个数据输出时间存入b数组
b[a[i]]=i;
for(i=1;i<m;i++)//需要输出的每个数据输出的时间
printf("%d ",b[i]);
printf("%d\n",b[m]);
}
return 0;
}
相关文章推荐
- 简谈多态及虚函数
- HTTP请求方法
- 编程之美3:最大公约数问题
- 网络编程之select
- form 转json,将form表单中的数据序列化数组后转换为Json
- MySQL入门--修改mysql提示符的两种方法
- OC 对象作为方法的参数连续传递
- ZOJ 3228 Searching the String
- Linux日常维护命令
- 记mysql 之 delete where 一张很大(20G)的表
- Nginx配置
- C语言按要求打印数组
- ViewPager + Fragment 获取Tag
- java基础学习总结——面向对象
- hdu2767&&hdu3836 Proving Equivalences(Tarjan+缩点)
- 基于注解的组件扫描
- Linux下安装VMware Tools 的方法
- 登录超时跳转页面失败和ajax传递到后台数据乱码
- Codeforces Round #353 (Div. 2)E
- SSM小技巧(一)、Controller中互相调用session中存储的内容