博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA120-Stacks of Flapjacks(思维)
阅读量:4608 次
发布时间:2019-06-09

本文共 2972 字,大约阅读时间需要 9 分钟。

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 
2 3 using namespace std; 4 5 vector
num, sort_num; 6 vector
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
());31 cnt = num.size();32 33 for (int i = 0; i < cnt; i++) {34 int x = sort_num[i];35 vector
::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\n", 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("\n");48 }49 vector
::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("\n");56 }57 for (int i = 0; i < ans.size(); i++) {58 printf("%d ", ans[i]);59 }60 printf("0\n");61 }62 return 0;63 }

 

转载于:https://www.cnblogs.com/npugen/p/9610786.html

你可能感兴趣的文章
MapReduce的ReduceTask任务的运行源码级分析
查看>>
Morning Reading Collection
查看>>
Sudo
查看>>
JS案例之8——从一个数组中随机取数
查看>>
C#中Dictionary小记
查看>>
mysql日期类型默认值'0000-00-00'容错处理
查看>>
openni和骨架追踪 rviz查看---34
查看>>
防止网站被iframe调用
查看>>
B - 畅通工程(并查集)
查看>>
linux使用rz、sz快速上传、下载文件
查看>>
基础练习 Huffuman树
查看>>
8.28笔记
查看>>
[转]springSecurity源码分析—DelegatingFilterProxy类的作用
查看>>
Bootstrap 时间控件timepicker与datepicker
查看>>
Net文本编辑器,语法高亮,折叠,自动补全,提示
查看>>
【转】用emWin进度条控件做个表盘控件,效果不错
查看>>
emwin之创建窗口与窗口回调函数的句柄是一致的
查看>>
JAVA笔记(基本数据类型)
查看>>
判断数字的正则表达式
查看>>
CAN通信工作原理个人心得
查看>>