Uva - 120 - Stacks of Flapjacks
2015-06-21 10:41
134 查看
选择排序,从大到小顺序一次排列,每次先把第 i 大的翻到最上面,然后在翻到正确的位置。
AC代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cctype> #include <cstring> #include <string> #include <sstream> #include <vector> #include <set> #include <map> #include <algorithm> #include <stack> #include <queue> #include <bitset> #include <cassert> #include <cmath> using namespace std; const int maxn = 35; int n, pancakes[maxn]; void filp(int p) { for (int i = 0; i < p - i; i++) { swap(pancakes[i], pancakes[p - i]); } cout << n - p << " "; } int main() { ios::sync_with_stdio(false); string s; while (getline(cin, s)) { // 不知道一次有多少个,直接读取一行 cout << s << endl; stringstream ss(s); n = 0; while (ss >> pancakes ) { n++; } for (int i = n - 1; i > 0; i--) { int maxP = max_element(pancakes, pancakes + i + 1) - pancakes; if (maxP == i) { // 如果最大元已经在最底部,不用翻转了 continue; } if (maxP > 0) { filp(maxP); // 大最大元先翻到最上面 } filp(i); // 然后翻到正确的位置 } cout << "0\n"; } return 0; }
相关文章推荐
- hypercall的实现机制与硬件虚拟化
- 本地推送服务 UILocalNotification
- 补发《超级迷宫》站立会议五
- 洗刷刷OO三A
- HDU_4609_3-idiots
- JavaScript-this的指向
- 剑指offer 面试题2
- 补发《超级迷宫》站立会议四
- java笔记30 网络编程2 服务器
- android onInterceptTouchEvent和onTouchEvent
- 基于高性能的硬件配置Nginx
- 使用python的Paramiko模块登陆SSH
- 对象切割 - 常量引用传递
- 餐饮美食图片数据表
- StrongReference, SoftReference, WeakReference 图解说明
- hdu 5272
- 本地推送UILocalNotification
- 【读书笔记:C++ primer plus 第六版 中文版】第2章:开始学习C++
- Android在ExpandableListView控制的基本使用
- 反射取子类、父类 属性