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;
    }

     

  • 相关阅读:
    Qt中实现单例模式(SingleTon)
    毕设开发手记(二)
    Qt中由表中单元格的QModelIndex获取Global Pos的正确方法
    Qt的槽可以使用默认参数
    C#结构的了解
    dos命令集江南技术联盟
    C#中的abstract与virtual的用法
    [转]六种删除数据库重复行的方法
    PL/SQL的TO_CHAR()与TO_DATE()
    索引索引索引(转)
  • 原文地址:https://www.cnblogs.com/zsyacm666666/p/4652588.html
Copyright © 2011-2022 走看看