UVa 1647 - Computer Transformation 解题心得

这个题目。。。。

想上题意

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 

冒号后有空格,数字前面有空格,数字后面有逗号,逗号后面没空格。。。想把题目过了就必须要仔细仔细得注意这些啊。。。

 

我的代码

 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

 

posted @ 2015-07-16 21:17  Shawn_Ji  阅读(284)  评论(0编辑  收藏  举报