NYOJ714-Card Trick

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

来源
第六届河南省程序设计大赛

 

 

题目大意是:有n张牌,找到一个顺序,使得第一次把上面一张取出放到最下面,

然后取出最上面一张是一,第i次把上面i张取出放到最下面,然后取出一张是i;

//模拟即可

#include<stdio.h>
#include<string.h>
int a[20],b[20];
void MoveBack(int n,int m)//将第一张牌移到牌堆最后
{
    int i;
 for(i=1;i<m;i++)
 {
  a[i]=a[i+1];
 }
 a[m]=n;
}
void DelTop(int m)//删除第一张牌
{
 int i;
 for(i=1;i<=m;i++)
 {
  a[i]=a[i+1];
 }
}
int main()
{
 int i,j,n,m,k,v,x,flag;
 scanf("%d",&n);
 while(n--)
 {
  scanf("%d",&m);
  for(i=1;i<=m;i++)
     a[i]=i;
  x=1;k=1;flag=m;
  while(m)
  {
     v=x;
     while(v--)
     MoveBack(a[1],m);
     b[a[1]]=k++;
     DelTop(m--);
     x++;
  }
  printf("%d",b[1]);
  for(i=2;i<=flag;i++)
   printf(" %d",b[i]);
  puts("");
  memset(a,0,sizeof(a));
  memset(b,0,sizeof(b));
 }
 return 0;
}
时间: 2024-08-01 16:03:47

NYOJ714-Card Trick的相关文章

Cloud Card能否干掉App

感谢同事[向西]投递本稿 云OS 3.0已发布,总算向外界表达了我们想做个啥,很多人也开始质疑,Cloud Card到底是个啥?云OS 3.0算不算自主研发的OS?等等,今天想就Cloud Card能否干掉App这个主题聊聊这些事情. 昨天三丰和我们开会讲到一个观点,IOS&Android以App为核心的OS过时了,用户使用各种移动设备,要的不是App,是服务,虽然App也是承载服务的形式,但App与App之间是割裂的.仔细想想确实如此,我们现在为了做一件事情往往需要多个App来回折腾,用户装的

LZOP解压查看文件小trick的过程

我们的HDFS中有一部分落地数据是用LZO格式来压缩的(另一部分采用gz压缩的RCFile,MapReduce中间结果采用snappy压缩).第一是因为它的压缩比和压缩/解压速度综合下来比较令人满意,第二是它只需要少量effort就能支持可切分(生成LZO文件后跑单机或分布式建索引程序),这样能充分利用MapReduce分而治之的编程思想和数据本地性. 之前要查看已经put到HDFS的LZO文件,都是先get到本地,再用lzop命令解压出来查看,但是有时候我们只是想预览下lzo文件的几行信息,前

OpenRisc入门(30) SD card controller模块分析与验证

引言 ORPSoC的硬件平台是包含SD card controller控制器的,但是对应的linux里面却没有对应的linux的驱动程序,这使ORPSoC的SD card的使用收到了很大的限制.没有驱动,硬件是不能工作的,SD卡控制器的驱动,linux提供了非常好的framework,在写驱动时只要开发者=关心最底层的部分,就是和硬件直接打交道的部分,即linuxMMC framework的HOST部分. 本小节并不介绍linux的MMC的framework,而把注意力放在核心部分,即直接对硬件

Ruby方法名简写的trick……(外加方法名缩写的trick)

刚才跟NS老兄聊的时候他给我看了段代码: night_stalker 写道 看到一个方法简写 trick Irb代码 irb(main):004:0> "Hello World".spl => ["Hello", "World"] irb(main):005:0> "Hi there Daniel".sp => ["Hi", "there", "Dani

CLR探索系列-GC中的Card table和Brick Table(垃圾回收系列)

在CLR的垃圾回收子系统中,Card Table和Brick Table是两个比较有意思的表. 在GC的过程中,一个Heap在运行了一段时间以后,已经分配的空间就会越来越大.在进行了一次局部代或者是完全的垃圾回收以后,就会涉及到一个GC堆的类似碎片整理的概念.整理优化一次GC Heap.同时,这种机制保证了譬如一个IIS Server在长时间的运行过程中的稳定性并且优化了其内存管理. 这样的好处是显而易见的,但是采用这种解决方案带来的问题也很容易想到:譬如一个存在于GC Heap里面的"Smal

android获取监听SD Card状态的方法

    android获取监听SD Card状态的方法 本文实例讲述了android获取监听SD Card状态的方法.分享给大家供大家参考.具体分析如下: 1. 注册StorageEventListener来监听SD卡状态即onStorageStateChanged()方法,当sd卡状态改变时,调用该方法. 复制代码 代码如下: public void onStorageStateChanged(String path,String oldState,String newState){ if (

大家有了解java card VM的吗?

问题描述 最近要做一个java card上的项目 以前没有接触过这方面的东西 希望能得到大家的帮助 网上关于java card的资料少的可怜 可以这方面的推荐一些资料吗?谢谢大家啊 解决方案 http://avery-leo.iteye.com/blog/378983这里有三篇文章!中文! 图文并茂! 希望对你有帮助!解决方案二:[url]http://jcp.org/http://jcp.org/jsr/tech/j2me.jsphttp://www.sun.com/software/jini

Structuring the Backend Service Architecture of a Mobile Card Game

Abstract: 2014 saw the rise of intense action mobile card games, and 2015 ushered in the age of real-time battles. Meanwhile, card games will face a strong challenge from intense mobile games. Confronted with the surging popularity of RPG games, mobi

image-Android中SD card存储图片问题

问题描述 Android中SD card存储图片问题 我有一个小问题,我用下面的代码在SD card中保存图片: public String SDSave( ) { //View arg0 // TODO Auto-generated method stub OutputStream outStream = null; File file = new File( extStorageDirectory AdName + "".PNG""); try { outStr