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个数列全部反转,要求把数列排序为升序. 算法点破后不值几钱... 只要想办法把最大的煎饼放到最后一个,然后就变成前面那些煎饼的数列的子题目了.递归或循环即 ...
随机推荐
- bzoj千题计划139:bzoj2229: [Zjoi2011]最小割
http://www.lydsy.com/JudgeOnline/problem.php?id=2229 最小割树介绍:http://blog.csdn.net/jyxjyx27/article/de ...
- 51nod 1181 质数中的质数
1181 质数中的质数(质数筛法) 题目来源: Sgu 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 如果一个质数,在质数列表中的编号也是质数,那么就称 ...
- Idea+TestNg配置test-output输出(转)
说明:testNG的工程我是使用eclipse创建的,直接导入到idea中,运行test时不会生产test-output,只能在idea的控制台中查看运行结果,然后到处报告,经过不懈的百度终于找到怎么 ...
- Docker中执行Shell出现乱码
问题描述 最近遇到一个问题: 执行命令 docker exec f4af9b sh -c 'bash /tmp/build.sh' 命令在docker中执行shell,会出现中文乱码的问题.但是在do ...
- 【我们开发有力量之二】利用javascript制作批量网络投票机器人(自动改IP)
帮朋友忙网络投票,粗粗地看了下,投票没有什么限制,仅有一个ip校验:每天每个ip仅能投票一次. 也就是说,可以写一个程序,自动更换IP地址(伪造IP地址),实现批量刷票的目的.于是我写了一个投票机器人 ...
- 【Linux】SecureCRT连接Linux乱码
SecureCRT连接linux出现乱码问题.解决方法. 打开SecureCRT-->option-->Session option
- windows 下 react-native(v0.56) Android 环境搭建踩坑记录
debugservicereact-native 安装官网 https://reactnative.cn/docs/getting-started.html 根据官网步骤一步步执行下去.还能碰到一些问 ...
- 【leetcode 简单】 第九十一题 找不同
给定两个字符串 s 和 t,它们只包含小写字母. 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母. 请找出在 t 中被添加的字母. 示例: 输入: s = "abcd&quo ...
- xgboost与sklearn的接口
xgb使用sklearn接口(推荐) XGBClassifier from xgboost.sklearn import XGBClassifier clf = XGBClassifier( sile ...
- GRUB (简体中文)
原文链接:https://wiki.archlinux.org/index.php/GRUB_(%E7%AE%80%E4%BD%93%E4%B8%AD%E6%96%87) 前言 引导程序是计算机启动时 ...