標籤:
這個題目。。。。
想上題意
10935 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
我的分(吐)析(槽):
這個題目,看題意就知道非常適合用STL中的deque容器,因為要頻繁地頭尾兩端操作嘛
明顯水題,,,但是我還是被搞的要死,因為格式,,因為格式,,因為格式 要說三遍
最後的輸出 要仔細地看 Discarded cards: 1, 3, 5, 2, 6
冒號後有空格,數字前面有空格,數字後面有逗號,逗號後面沒空格。。。想把題目過了就必須要仔細仔細得注意這些啊。。。
My Code
1 #include<cstdio> 2 #include<iostream> 3 #include<deque> 4 #include<vector> 5 using namespace std; 6 7 deque<int> cards; 8 vector<int> dis; 9 int main()10 {11 vector<int>::iterator it;12 void change(deque<int> & c);13 int n;14 while (cin >> n&&n != 0){15 dis.clear();16 cards.clear();17 for (int i = 0; i < n; i++){18 cards.push_back(i+1);19 } //容器填充 序列產生20 for (int i = 0; i < n-1;i++)21 change(cards);22 cout << "Discarded cards: ";23 for (it = dis.begin(); it != dis.end(); it++){24 cout << *it;25 if (it != dis.end() - 1) cout << ‘, ‘;26 }27 cout << endl;28 cout <<"Remaining card: " <<cards.front()<<endl;29 }30 return 0;31 32 33 }34 35 void change(deque<int> & c)36 {37 dis.push_back(c.front());38 c.pop_front();39 int temp = c.front();40 c.pop_front();41 c.push_back(temp);42 }View Code
UVa 1647 - Computer Transformation 解題心得