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个数列全部反转,要求把数列排序为升序. 算法点破后不值几钱... 只要想办法把最大的煎饼放到最后一个,然后就变成前面那些煎饼的数列的子题目了.递归或循环即 ...
随机推荐
- [ONTAK2015]Bajtman i Okrągły Robin
bzoj 4276: [ONTAK2015]Bajtman i Okrągły Robin Time Limit: 40 Sec Memory Limit: 256 MB Description 有 ...
- 经典幻灯片插件Swiper
照着写的demo,搞清楚什么叫分页器Pagination,什么叫nav,搞清楚DOM结构,container,wrapper之类的,就能写了.效果掉渣天! <!DOCTYPE html> ...
- IFrame跨域处理方法-Javascript
在漫长的前端开发旅途上,无可避免的会接触到ajax,而且一般情况下都是用在同一域下的ajax请求:但是如果请求是发生在不同的域下,请求就无法执行,并且会抛出异常提示不允许跨域请求,目前我没有找到明确的 ...
- Redmine数据表字段说明
Redmine数据表结构 issues tracker_id 跟踪标签的id,外键到trakers表 project_id 外键到项目project subject 主题 description 问题 ...
- python概念-Socket到底有多骚
Socket究竟是什么呢? 简单来说Socket就是用来完成客户端与服务器之间的通信 例如浏览器访问网页,例如网络游戏等一切基于客户端服务器来实现的C/S架构程序 Socket是基于互联网OSI七层协 ...
- SQL Server 2000事务复制问题
2000现在用的估计不多了,把之前收集的一些复制问题整理发布出来.可能都是些很白很二的问题,但人总是由最初的无知不断成长●-● SQL Server 2000事务复制问题服务器A(发布) 服务器B(分 ...
- react 带参数事件方法不立即执行
handleClick:()=>{this.to_step('to_step3_2')}}
- 面试:----Nginx的一理解
1.静态HTTP服务器 首先,Nginx是一个HTTP服务器,可以将服务器上的静态文件(如HTML.图片)通过HTTP协议展现给客户端. 配置: 2.反向代理服务器 什么是反向代理? 客户端本来可以直 ...
- scrapy 爬虫踩过的坑(II)
同事写了个爬虫,逻辑上看着没什么问题,但是一直报错:Request url must be str or unicode…… 打印了一下url 和url 的类型,确实是 unicode 懵逼…… 打印 ...
- 数据库名(DB_NAME)、实例名(Instance_name)、以及操作系统环境变量(ORACLE_SID)
数据库名(DB_NAME).实例名(Instance_name).以及操作系统环境变量(ORACLE_SID) 在ORACLE7.8数据库中只有数据库名(db_name)和数据库实例名(instanc ...