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个数列全部反转,要求把数列排序为升序. 算法点破后不值几钱... 只要想办法把最大的煎饼放到最后一个,然后就变成前面那些煎饼的数列的子题目了.递归或循环即 ...
随机推荐
- StringBuffer类和String 类的 equals 和 ==
注意: equals(Object obj)equals方法的参数是任意对象 Object类的equals方法就是用==判断的,即判断两个对象是否为同一个对象 StringBuffer类没有重写equ ...
- 【转载】BP神经网络
原文地址:http://blog.csdn.net/acdreamers/article/details/44657439 今天来讲BP神经网络,神经网络在机器学习中应用比较广泛,比如函数逼近,模式识 ...
- WCF: Retry when service is in fault state.
Service Host: using System; using System.Configuration; using System.ServiceModel; using System.Serv ...
- 【实操笔记】MySQL主从同步功能实现
写在前边: 这两天来了个需求,配置部署两台服务器的MySQL数据同步,折腾了两天查了很多相关资料,一直连不上,后来发现其实是数据库授权的ip有问题,我们用的服务器是机房中的虚拟机加上反向代理出来的,坑 ...
- [hadoop]hadoop2.6完全分布式环境搭建
在经过几天的环境搭建,终于搭建成功,其中对于hadoop的具体设置倒是没有碰到很多问题,反而在hadoop各节点之间的通信遇到了问题,而且还反复了很多遍,光虚拟机就重新安装了4.5次,但是当明白了问题 ...
- 20155214 2016-2017-2 《Java程序设计》第7周学习总结
20155214 2016-2017-2 <Java程序设计>第7周学习总结 教材学习内容总结 UTC时间以Unix元年(1970年)为起点经过的秒数. ISO 8601并非年历系统,大部 ...
- sql server中的日期函数
DATEADD 在向指定日期加上一段时间的基础上,返回新的 datetime 值. 语法 DATEADD ( datepart , number, date ) 参数 (1) ...
- git 配置 SSH密钥
1.登录用户 $ git config --global user.name "geekfeier" $ git config --global user.email " ...
- 使用转义防御XSS
使用转义防御XSS 在输出的时候防御XSS即对用户输入进行转义,XSS的问题本质上还是代码注入,HTML或者javascript的代码注入,即混淆了用户输入的数据和代码.而解决这个问题,就需要根据用户 ...
- 20155303 2016-2017-2 《Java程序设计》第四周学习总结
20155303 2016-2017-2 <Java程序设计>第四周学习总结 教材学习内容总结 第六章 继承与多态 6.1 何谓继承 继承避免多个类间重复定义共同行为,使用关键字exten ...