8-1 Stacks of Flapjacks UVA120
题意: 有一叠煎饼在锅里 n n<=30张 每张都有一个数字 代表它的大小 厨师每次可以选择一个数k 把从锅底开始数第k张上面的煎饼全部反过来 即原来的在上面的煎饼现在到了下面 如
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
按照选择排序模拟
各种加一减一模拟的好乱 糟糕的代码
#include<bits/stdc++.h>
using namespace std;
#define N 30
int a[N];
int path[N];
int n,c; void chang(int x)
{
int temp[N];
for(int i=;i<=n-x+;i++)
temp[i]=a[n-x-i+];
for(int i=n-x+;i<=n;i++)
temp[i]=a[i];
memcpy(a,temp,sizeof a);
} void dfs(int cur)
{
if(cur==)
{
for(int i=;i<c;i++)
printf("%d ",path[i]);
printf("0\n");
return ;
}
int maxx=;
int u=;
for(int i=;i<=cur;i++)
{
if(a[i]>maxx){maxx=a[i],u=i; }
}
if(u!=cur)
{
if(u!=)
chang(n-u+);path[c++]=n-u+;
chang(n-cur+);path[c++]=n-cur+;
}
dfs(cur-);
} int main()
{
char s[];
while(fgets(s,,stdin))
{
int cnt=;
int v=;
int i=;
while(i<strlen(s))
{
while(!isspace(s[i])&&i<strlen(s))v=v*+s[i++]-'';
a[cnt++]=v;
v=;
i++;
} n=cnt-;
for(int i=;i<=n;i++)
{ printf("%d",a[i]);
if(i!=cnt)printf(" ");
}
printf("\n");
c=;
dfs(n);
}
}
数据读入用流来写很快!!! 还有找最大值下标函数!! 交换的话直接一个循环 加swap函数 不用temp
我写了70行 lrj大大写了30行
菜鸡还需努力。。。
#include<bits/stdc++.h>
using namespace std;
const int maxn = + ;
int n, a[maxn]; // 翻转a[0..p]
void flip(int p) {
for(int i = ; i < p-i; i++)
swap(a[i], a[p-i]);
printf("%d ", n-p);
} int main() {
string s;
while(getline(cin, s)) {
cout << s << "\n";
stringstream ss(s);
n = ;
while(ss >> a[n]) n++;
for(int i = n-; i > ; i--) {
int p = max_element(a, a+i+) - a; // 元素a[0..i]中的最大元素
if(p == i) continue;
if(p > ) flip(p); // flip(0)没啥意思,是不?
flip(i);
}
printf("0\n");
}
return ;
}
8-1 Stacks of Flapjacks UVA120的更多相关文章
- 【思维】Stacks of Flapjacks
[UVa120] Stacks of Flapjacks 算法入门经典第8章8-1 (P236) 题目大意:有一个序列,可以翻转[1,k],构造一种方案使得序列升序排列. 试题分析:从插入排序即可找到 ...
- uva 120 stacks of flapjacks ——yhx
Stacks of Flapjacks Background Stacks and Queues are often considered the bread and butter of data ...
- UVaOJ 120 - Stacks of Flapjacks
120 - Stacks of Flapjacks 题目看了半天......英语啊!!! 好久没做题...循环输入数字都搞了半天...罪过啊!!! 还是C方便一点...其实C++应该更方便的...C+ ...
- Uva 120 - Stacks of Flapjacks(构造法)
UVA - 120 Stacks of Flapjacks Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld &a ...
- uva Stacks of Flapjacks
Stacks of Flapjacks 题目链接:Click Here~ 题目描写叙述: ...
- Stacks of Flapjacks(栈)
Stacks of Flapjacks Background Stacks and Queues are often considered the bread and butter of data ...
- Stacks of Flapjacks
Stacks of Flapjacks Background Stacks and Queues are often considered the bread and butter of data s ...
- UVa120 - Stacks of Flapjacks
Time limit: 3.000 seconds限时:3.000秒 Background背景 Stacks and Queues are often considered the bread and ...
- Uva120 Stacks of Flapjacks 翻煎饼
水水题.给出煎饼数列, 一次只能让第一个到第i个数列全部反转,要求把数列排序为升序. 算法点破后不值几钱... 只要想办法把最大的煎饼放到最后一个,然后就变成前面那些煎饼的数列的子题目了.递归或循环即 ...
随机推荐
- M-JPEG、MPEG4、H.264都有何区别
压缩方式是网络视频服务器和网络摄像机的核心技术,压缩方式很大程度上决定着图像的质量.压缩比.传输效率.传输速度等性能,它是评价网络视频服务器和网络摄像机性能优劣的重要一环.随着多媒体技术的发展,相继推 ...
- Vuex深入理解
store下的index.js: import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) let store = new Vuex.St ...
- Dubbo学习笔记3:Dubbo管理控制台与监控中心的搭建
Dubbo源码下载与编译 本文来讲下如何在Dubbo源码编译后,得到用于搭建管理控制台的war包和监控平台的jar包. 首先需要到Github上下载Dubbo的源码,解压后如下: 在dubbo-2.5 ...
- OpenResty 扩展库之(一)——lua-resty-shell 库
介绍 当您需要执行子进程(或shell命令)时,这是一个打算与OpenResty应用程序一起使用的小型库. 它类似于os.execute和io.popen,除了它是完全非阻塞的,因此即使对于需要很长时 ...
- Web应用开发中的几个问题
Introduction 由于Ajax技术在Gmail中的成功应用和高性能的V8引擎的推出使得编写Web应用变得流行 起来,使用前端技术也可以编写具有复杂交互的应用.相对于native应用,Web应用 ...
- [CodePlus 2017 11月赛&洛谷P4058]木材 题解(二分答案)
[CodePlus 2017 11月赛&洛谷P4058]木材 Description 有 n棵树,初始时每棵树的高度为 Hi ,第 i棵树每月都会长高 Ai.现在有个木料长度总量为 S的订单, ...
- select标签的描述
1.标签html与js如下代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"&g ...
- 兴人类TDD培训札记
兴人类TDD培训札记 恰同学少年,风华正茂:书生意气,挥斥方遒 -- <沁园春 长沙> 幸之 前不久,非常幸运地全程参与了公司与南京5所知名高校合作的"兴人类TDD培训" ...
- Groovy/Spock 测试导论
Groovy/Spock 测试导论 原文 http://java.dzone.com/articles/intro-so-groovyspock-testing 翻译 hxfirefox 测试对于软件 ...
- windebug分析高cpu问题
分析高CPU的关键是找到哪个线程是持续运行,占用CPU时间. 可以隔上两分钟连续抓两个dump文件,使用 !runaway 查看线程运行的时间 通过对比两个dump文件的线程时间,看看哪个线程运行的时 ...