zoukankan      html  css  js  c++  java
  • Throwing cards away I


                                                                               Throwing cards away I 
      Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is performed as long as there are at least two cards in the deck:
      Throw away the top card and move the card that is now on the top of the deck to the bottom of the deck. 
    Your task is to find the sequence of discarded cards and the last, remaining card. 

    Input Each line of input (except the last) contains a number n ≤ 50. The last line contains ‘0’ and this line should not be processed. 

    Output For each number from the input produce two lines of output. The first line presents the sequence of discarded cards, the second line reports the last remaining card. No line will have leading or trailing spaces. See the sample for the expected format. 
    Sample Input 
    7 19 10 6 0 
    Sample Output
    Discarded cards: 1, 3, 5, 7, 4, 2 
    Remaining card: 6 
    Discarded cards: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 4, 8, 12, 16, 2, 10, 18, 14 
    Remaining card: 6 
    Discarded cards: 1, 3, 5, 7, 9, 2, 6, 10, 8 
    Remaining card: 4 
    Discarded cards: 1, 3, 5, 2, 6 
    Remaining card: 4
     
     
     
    题目大意:
    一副牌完全按顺序排好,然后你要做的就是每次扔掉最前面一张牌,再把此时最前的一张牌放到最后面去,最后输出你扔的那些牌和最后剩下的牌
     
     
    思路:
    典型的队列,用队列模拟就好。
     
    代码:
     
     
    #include"iostream"
    using namespace std;
    const int maxn=2600;
    int a[maxn];
    int n;
    
    void Work()
    {
    for(int i=1;i<=n;i++)
    a[i]=i;
    int head=1;
    int tail=n;
    if(tail==1)
    cout<<endl<<"Remaining card: "<<a[head]<<endl;
    else
    {
    cout<<' ';
    while(head<tail)
    {
    cout<<a[head++];
    if(head<tail) cout<<", ";
    a[++tail]=a[head++];
    if(head==tail) {cout<<endl<<"Remaining card: "<<a[head]<<endl;}
    }
    }
    }
    
    
    int main()
    {
    while(cin>>n&&n)
    {
    	cout<<"Discarded cards:";
    Work();
    }
    return 0;
    }

     

  • 相关阅读:
    HDU 1016 Prime Ring Problem
    CreateRemoteThread函数多參数传入用法
    Dynamics CRM2015 on-premises直接升级Dynamics CRM2016 on-premises
    cocos2d-x+lua代码热载入(Hot Swap)的研究
    DirectX 9.0c游戏开发手记之“龙书”第二版学习笔记之8: Chap10: Lighting
    js合并table单元格(拼table的时候并不知道详细几行几列)
    简单图模板 Graph
    POJ-3278-Catch That Cow-广搜(BFS)
    android用存到缓存的方法来保存ListView里的数据
    Ubuntu 14.10中连接Win10的smb共享文件
  • 原文地址:https://www.cnblogs.com/zsyacm666666/p/4652588.html
Copyright © 2011-2022 走看看