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
题目类型:栈模拟
算法分析:按照题目所说的方式进行栈模拟,将会discard的cards数字压入输出数组output中,然后直接遍历数组输出,下面第一份代码是更好的实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
/************************************************** filename :c.cpp author :maksyuki created time :2018/1/22 20:09:59 last modified :2018/1/22 20:27:01 file location :C:\Users\abcd\Desktop\TheEternalPoet ***************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("in", "r", stdin) #define CFO freopen ("out", "w", stdout) #define CPPFF ifstream cin ("in") #define CPPFO ofstream cout ("out") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define DBT printf("time used: %.2lfs\n", (double) clock() / CLOCKS_PER_SEC) #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; int sta[maxn], pt, pb; vector<int> ans; int main() { #ifdef LOCAL CFF; //CFO; #endif int n; while(scanf(" %d", &n) != EOF) { if(!n) break; ans.clear(); pt = pb = 1; for(int i = 1; i <= n; i++) sta[pb++] = i; pb--; while(pt < pb) { ans.emplace_back(sta[pt++]); sta[++pb] = sta[pt]; pt++; } printf("Discarded cards:"); int len = ans.size(); for(int i = 0; i < len; i++) { if(!i) printf(" %d", ans[i]); else printf(", %d", ans[i]); } puts(""); printf("Remaining card: %d\n", sta[pb]); } return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#include <iostream> #include <fstream> #include <queue> using namespace std; const int maxn = 666; int output[maxn]; int len; int main() { // ifstream cin ("aaa.txt"); int num; while (cin >> num) { if (num == 0) break; queue <int> ans; int i; for (i = 1; i <= num; i++) ans.push (i); len = 0; while (ans.size () >= 2) { int temp_a = ans.front (); output[len++] = temp_a; ans.pop (); temp_a = ans.front (); ans.pop (); ans.push (temp_a); } output[len++] = ans.front (); if (num == 1) { cout << "Discarded cards:" << endl << "Remaining card: " << output[0] << endl; } else { cout << "Discarded cards:"; for (i = 0; i < len - 2; i++) cout << " " << output[i] << ","; cout << " " << output[i] << endl; cout << "Remaining card: " << output[len-1] << endl; } } return 0; } |