Throwing cards away I uva1594



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.

Each line of input (except the last) contains a number  n  ≤ 50. The last line contains 0 and this line should not be processed. 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

Output for sample input

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<cstdio>
using namespace std;
int q[200];
int main()
{
    int head,tail,x;
    while(scanf("%d",&x)!=EOF&&x!=0)
    {

        for(int i=1;i<=x+1;i++)
        q[i]=i;
        head=1;
        tail=x+1;
        printf("Discarded cards:");
        while(head<tail-1)
        {
        if(head<tail-2)
          printf(" %d,",q[head]);
          else
            printf(" %d",q[head]);
             head++;
             q[tail]=q[head];
        tail++;
          head++;
        }
        printf("\n");
       printf("Remaining card:");
        printf(" %d",q[head]);
        printf("\n");

    }
    return 0;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-26 02:20:00

Throwing cards away I uva1594的相关文章

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 t

紫书第五章训练3 D - Throwing cards away I

D - 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

UVa---------10935(Throwing cards away I)

题目: Problem B: 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 car

UVA10940 Throwing cards away II【数学规律+约瑟夫环】

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 th

UVa 10935 - Throwing cards away I

模拟队列操作.  注意当n == 1时第一行输出末尾没有空格.PE一次~~~ 代码  : import java.util.*; public class Main10935 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); Queue<Integer> q = new LinkedList<Integer>(); while(true) { int n = scan.

UVa 10935 Throwing cards away I【队列】

题意:给出 n张牌,从上往下编号依次为1到n,当牌的数目至少还剩下2张时,把第一张牌扔掉,然后把新的一张牌放在牌堆的最底部,问最后剩下的那一张牌是哪一张牌. 模拟队列的操作------- 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 #include<queue> 7 usin

UVa 10935 (水题) Throwing cards away I

直接用STL里的queue模拟即可. 1 #include <cstdio> 2 #include <queue> 3 using namespace std; 4 5 const int maxn = 60; 6 int discarded[maxn], cnt; 7 8 int main() 9 { 10 int n; 11 while(scanf("%d", &n) == 1 && n) 12 { 13 cnt = 0; 14 qu

UVA 10940 Throwing cards away II

题意略: 先暴力打表发现规律 N=1 ans=1N=2 ans=2N=3 ans=2N=4 ans=4N=5 ans=2N=6 ans=4N=7 ans=6N=8 ans=8N=9 ans=2N=10 ans=4N=11 ans=6N=12 ans=8N=13 ans=10N=14 ans=12N=15 ans=14N=16 ans=16N=17 ans=2N=18 ans=4N=19 ans=6N=20 ans=8N=21 ans=10N=22 ans=12N=23 ans=14N=24 an

UVA10940 - Throwing cards away II(找规律)

题目链接 题目大意:桌上有n张牌,按照1-n的顺序从上到下,每次进行将第一张牌丢掉,然后把第二张放到这叠牌的最后.反复进行这样的操作,直到只剩下一张牌. 解题思路:只能先暴力,将前面小点的n打印出来,看看有什么规律. 规律:f[2^k + mod] = 2*mod;(mod > 0); n = 1需要特判. 代码: #include <cstdio> #include <cstring> const int maxn = 5e5 + 5; int f[maxn]; void