zoukankan      html  css  js  c++  java
  • UVA120-Stacks of Flapjacks(思维)

    Problem UVA120-Stacks of Flapjacks

    Accept: 9232  Submit: 38455
    Time Limit: 10000 mSec

    Problem Description

    Input

    The input consists of a sequence of stacks of pancakes. Each stack will consist of between 1 and 30 pancakes and each pancake will have an integer diameter between 1 and 100. The input is terminated by end-of-file. Each stack is given as a single line of input with the top pancake on a stack appearing first on a line, the bottom pancake appearing last, and all pancakes separated by a space.

     Output

    For each stack of pancakes, the output should echo the original stack on one line, followed by some sequence of flips that results in the stack of pancakes being sorted so that the largest diameter pancake is on the bottom and the smallest on top. For each stack the sequence of flips should be terminated by a ‘0’ (indicating no more flips necessary). Once a stack is sorted, no more flips should be made.

     

     Sample Input

    1 2 3 4 5
    5 4 3 2 1
    5 1 2 3 4
     

     Sample Output

    1 2 3 4 5

    0

    5 4 3 2 1

    1 0

    5 1 2 3 4

    1 2 0

    题解:虽说是道脑洞题,但是稍加思考应该问题不大,第一想法先把大的搞定,因为大的一定就不用动了,相当于缩小了问题规模。有了这个想法肯定就去想怎么把最大的放到最下面,稍加尝试就出来了。

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 vector<int> num, sort_num;
     6 vector<int> ans;
     7 
     8 bool cmp(const int &a, const int &b) {
     9     return a > b;
    10 }
    11 
    12 void init() {
    13     ans.clear();
    14     num.clear();
    15     sort_num.clear();
    16 }
    17 
    18 int main()
    19 {
    20     //freopen("input.txt", "r", stdin);
    21     //freopen("output.txt", "w", stdout);
    22     string str;
    23     while (getline(cin, str)) {
    24         init();
    25         cout << str << endl;
    26         stringstream ss(str);
    27 
    28         int cnt, xx;
    29         while (ss >> xx) num.push_back(xx), sort_num.push_back(xx);
    30         sort(sort_num.begin(), sort_num.end(), greater<int>());
    31         cnt = num.size();
    32 
    33         for (int i = 0; i < cnt; i++) {
    34             int x = sort_num[i];
    35             vector<int>::iterator iter = num.begin();
    36             for (; iter != num.end(); iter++) {
    37                 if (*iter == x) break;
    38             }
    39             int pos = iter - num.begin();
    40             //printf("pos : %d
    ", pos);
    41             if (pos == cnt - i - 1) continue;
    42             if (pos != 0) {
    43                 iter++;
    44                 reverse(num.begin(),iter);
    45                 ans.push_back(cnt - pos);
    46                 //for (int i = 0; i < cnt; i++) printf("%d ", num[i]);
    47                 //printf("
    ");
    48             }
    49             vector<int>::iterator iter2 = num.begin();
    50             int k = cnt - i;
    51             while (k--) iter2++;
    52             reverse(num.begin(), iter2);
    53             ans.push_back(i + 1);
    54             //for (int i = 0; i < cnt; i++) printf("%d ", num[i]);
    55             //printf("
    ");
    56         }
    57         for (int i = 0; i < ans.size(); i++) {
    58             printf("%d ", ans[i]);
    59         }
    60         printf("0
    ");
    61     }
    62     return 0;
    63 }
  • 相关阅读:
    操作系统——死锁相关
    Java 实现广度优先搜索和深度优先搜索
    Java 实现常见排序算法
    初次接触JQuery
    Firefox使用stylish自定义网页背景
    使用randoop自动化生成测试用例
    软件测试(五)——使用Selenium IDE进行自动化测试
    软件项目管理(二)——用jenkins持续集成、Maven、Github的使用
    云计算(一)——使用 Hadoop Mapreduce 进行数据处理
    软件测试(四)——图覆盖
  • 原文地址:https://www.cnblogs.com/npugen/p/9610786.html
Copyright © 2011-2022 走看看