HDU 2319 Card Trick (模拟)
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.
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`2
4
5`
Sample Output`
2 1 4 3
3 1 4 5 2`
题意:
你的手中有1~n这n张扑克牌,但是这些牌的顺序是不确定的,你需要在这些牌上进行一些操作
1.第一次你从最上面拿出一张牌放到最下面,然后把当前的第一张牌上面的数字记下来并把这张牌拿出 去
2.第二次你要先从最上面取出一张牌放到最下面,再从最上面取出一张牌放到最下面,然后把当前的第一张牌上面的数字记下来并把这张牌拿出去
3.依次进行n次,不同之处在于第i次取最上面牌再放到最下面的操作要进行i次
最后要求你一次记下的这些牌的序列为1到n,让求手中牌的原始序列。
方法一
一种完全逆序的思维,可能比较难以理解一些:
#include<stdio.h>
#include<iostream>
#include<queue>
using namespace std;
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
int v[1000];
queue<int>q;
int m,i;
scanf("%d",&m);
for(i=1; i<=m; i++)
q.push(i);//将这些数字按照正确的顺序存储到队列中
int op;
int m1=1;
while(!q.empty())
{
for(i=1; i<=m1; i++)//依次进行相应的取放操作
{
op=q.front();
q.pop();
q.push(op);
}
int b=q.front();
q.pop();
v[b]=m1;/*让次数当作数组的值,取出来的数字当下标,
因为这样是一种与题意要求相反的顺序,
所以输出的时候下标当取出顺序,数组值当取出的数字,
就正好是与提上要求的正序
*/
m1++;
}
printf("%d",v[1]);
for(i=2;i<=m;i++)
printf(" %d",v[i]);//所有的都是反的!!!!!!
printf("\n");
}
return 0;
}
方法二:
完全逆序模拟题上的操作步骤,最后队列的逆序即为所求:
#include <iostream>
#include <queue>
using namespace std;
int main()
{
int n,a,t,b[15];
queue<int> Q;
cin>>n;
while(n--)
{
cin>>a;
//逆序将每一张牌放入进去,然后进行相应的操作
Q.push(a);//第一次也要进行n次循环,但是只有一个数,没有循环的必要
for(int i=1; i<a; i++)
{
Q.push(a-i) //放入一张新的牌;
for(int j=a-i; j>=1; j--) //进出a-i次
{
t=Q.front();
Q.pop();
Q.push(t);
}
}
int q=0;
while(!Q.empty()) //因为要逆序将队列中的值输出来,先把队列打印出来
{
b[q++]=Q.front();
Q.pop();
}
for(int i=q-1; i>0; i--)//逆序输出
cout<<b[i]<<" ";
cout<<b[0]<<endl;
}
return 0;
}
HDU 2319 Card Trick (模拟)的更多相关文章
- 【HDOJ】2319 Card Trick
水题,STL双端队列. /* 2319 */ #include <iostream> #include <cstdio> #include <cstring> #i ...
- UESTC 890 Card Trick(DP 纸牌魔术)
题意 给你一些牌 所有正面朝下放桌子上 你选一个起点 翻开那张牌 牌上的数字是几就向前走几步 J,Q,K 都是向前走10步 A向前走11步 知道向前走相应的步数后超过了终点 ...
- ny714 Card Trick
Card Trick 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 The magician shuffles a small pack of cards, holds ...
- SPOJ 1108 Card Trick 暴力模拟
解释一下样例,因为我觉得这个题意表述的不是很清楚.以第二组样例为例. 牌序为:3 1 4 5 2 第一轮:把 3 放到末尾:1 4 5 2 3,最顶上的牌是1,把1拿走.剩余 4 5 2 3 第二轮: ...
- POJ 2200 A Card Trick(模拟)
题目链接 题意 : 一共52张牌(A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K)花色分别是C,D,H,S ...给助理5张牌,然后助理需要重新排一下次序,把第一张牌给观 ...
- HDU 5510---Bazinga(指针模拟)
题目链接 http://acm.hdu.edu.cn/search.php?action=listproblem Problem Description Ladies and gentlemen, p ...
- HDU 3328 Flipper 栈 模拟
首先想说,英语太烂这题读了很长时间才读懂......题意是说输入有几张牌,然后输入这些牌的初始状态(是面朝上还是面朝下),然后输入操作方式,R表示翻一下右边的牌堆,L表示翻一下左边的牌堆,直到最后摞成 ...
- HDU 5047 Sawtooth(大数模拟)上海赛区网赛1006
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5047 解题报告:问一个“M”型可以把一个矩形的平面最多分割成多少块. 输入是有n个“M",现 ...
- HDU 5965 扫雷 【模拟】 (2016年中国大学生程序设计竞赛(合肥))
扫雷 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submissi ...
随机推荐
- 【第九周】beta-review阶段贡献分分配
组名: 新蜂 组长: 武志远 组员: 宫成荣 谢孝淼 杨柳 李峤 项目名称: java俄罗斯方块NEO 武志远 武志远 武志远 武志远 武志远 宫成荣 宫成荣 杨柳 宫成荣 宫成荣 李峤 杨柳 李峤 ...
- javascript之彻底理解prototype
prototype很简单, 就是提供一种引用的机制. let BaseObject = { toString(){ return this.str; }, }; let Object1 = {str: ...
- Privoxy
Privoxy 前沿: 这个玩意我以前都没听说过,今天在别人的帮助下试了试,只想说:谁还能阻挡我,我就是要USA....ps:在此感谢每一个帮助我的人 介绍: Privoxy是一款带过滤功能的代理服务 ...
- 基于Git制作电子书 GitBook
GitBook 详细介绍 GitBook 是一个基于 Node.js 的命令行工具,可使用 Github/Git 和 Markdown 来制作精美的电子书,GitBook 并非关于 Git 的教程. ...
- ONS发布订阅消息
ONS, 全名Open Notification Service, 是阿里基于开源消息中间件RocketMQ的一个云产品. 首先,要申请阿里账号等.本地也可以申请阿里云账号自己调试.此处为公司拥有阿里 ...
- MVC SignalR Hub初学
具体有关SignalR的说明查网上,我这里简单列举一个例子 1.新建MVC,添加SignalR引用(NuGet安装). 2.添加OWIN START类 public class Startup { p ...
- 【Python】Python对象类型及其运算
Python对象类型及其运算 基本要点: 程序中储存的所有数据都是对象(可变对象:值可以修改 不可变对象:值不可修改) 每个对象都有一个身份.一个类型.一个值 例: >>> a1 = ...
- action动作类的生命周期
创建:Action动作类每次请求的时候都会创建一个实例对象 销毁:当前action动作类的请求响应完后就消失了 跟javaweb中的HttpServletRequest的生命周期是一样的,struts ...
- Python敏感地址扫描和爬取工具
0×01 说明: 为了方便信息安全测评工作,及时收集敏感地址(初衷是爬取api地址),所以写了这么个小工具.两个简单的功能(目录扫描和url地址爬取). 0×02 使用参数: python spide ...
- BZOJ3771 Triple 【NTT + 容斥】
题目链接 BZOJ3771 题解 做水题放松一下 先构造\(A_i\)为\(x\)指数的生成函数\(A(x)\) 再构造\(2A_i\)为指数的生成函数\(B(x)\) 再构造\(3A_i\)为指数的 ...