zoukankan      html  css  js  c++  java
  • 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("
    ");
            }
            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;
        }
    
  • 相关阅读:
    Jsp的内置标签和jstl标签
    Jsp中的EL表达式
    JSP和servlet结合案例
    Jsp:内置对象和四种域对象的理解
    编码实战Web端联系人的增删改查
    Session案例
    Cookie案例分析
    会话数据的保存——cookie
    ServletContext和ServletConfig
    Servlet(1)
  • 原文地址:https://www.cnblogs.com/cmmdc/p/6729546.html
Copyright © 2011-2022 走看看