trowing cards away
Original question test instructions:
There are n cards, starting from the first card (the card at the top head), numbered 1 to n from the top down. When there are at least two cards left, do the following: Throw the first card away and place the new first card at the end of the stack (into tail). Lose as each row contains an n, and the output is discarded each time the card and the last remaining cards. Requirements: n<=50
test Instructions Analysis and Procedures :
Define an array of at least 2n-1 size, and make the array space a little larger to avoid errors. Set a variable head and tail subscript records the first and last data of the input array, each time moving it backward one position to the next array element.
The case output is as follows:
Intput:
7
Output
Discarded cards:1,3,5,7,4,2
Remaining Cards:6
The program code is as follows:
1#include <iostream>2#include <cstdio>3 using namespacestd;4 5 inta[ +];6 7 intMain ()8 {9 intN;Ten while(Cin >> N &&N) One { A for(inti =0; I <= N; ++i) -A[i] =i; - the intHead =1, tail = N, flag =1; -printf"Discarded cards:"); - while(Head <tail) - { + if(!flag) -printf","); +printf"%d", A[head + +]); Aa[++ Tail] = A[head + +]; atFlag =0; - } -printf"\nremaining card:%d\n", A[head]); - } - return 0; -}
-uva10935 title: trowing cards Away1 solution and simple analysis