时间限制:1 秒

内存限制:32 兆

特殊判题:是

提交:265

解决:116

题目描述:

We start with a stack n of pancakes of distinct sizes. The problem is to convert the stack to one in which the pancakes are in size order with the smallest on the top and the largest on the bottom. To do this, we are allowed to flip the top k pancakes over
as a unit (so the k-th pancake is now on top and the pancake previously on top is now in the k-th position).

For example: This problem is to write a program, which finds a sequence of at most (2n - 3) flips, which converts a given stack of pancakes to a sorted stack.

输入:

Each line of the input gives a separate data set as a sequence of numbers separated by spaces. The first number on each line gives the number, N, of pancakes in the data set. The input ends when N is 0 (zero) with no other data on the line. The remainder
of the data set are the numbers 1 through N in some order giving the initial pancake stack.

The numbers indicate the relative sizes of the pancakes. N will be, at most, 30.

输出:

For each data set, the output is a single-space separated sequence of numbers on a line. The first number on each line, K, gives the number of flips required to sort the pancakes. This number is followed by a sequence of K numbers, each of which gives
the number of pancakes to flip on the corresponding sorting step. There may be several correct solutions for some datasets. For instance 3 3 2 3 is also a solution to the first problem below.

样例输入:
3 1 3 2
5 4 3 2 5 1
0
样例输出:
3 2 3 2
3 3 4 5
来源:
2009年北京大学计算机研究生机试真题

思路:

著名的翻饼子游戏。要求最后状态是小饼在大饼的上面,求翻得次数。最多需要翻2n-3次。

这种题要用逆向思维以及递归方法来分析,考察最终状态及前一步,逐步往前考察,从而发现游戏思路。

代码:

#include <stdio.h>

#define N 30

int n, k;
int a[N+1], r[N+1], step[2*N+1]; void swap(int *x, int *y)
{
int tmp = *x;
*x = *y;
*y = tmp;
} void print(int *x)
{
int j;
for (j=1; j<n; j++)
printf("%d ", x[j]);
printf("%d\n", x[j]);
} void flip(int i)
{
int j;
for (j=1; j<=i/2; j++)
{
swap(&a[j], &a[i-j+1]);
r[a[j]] = j;
r[a[i-j+1]] = i-j+1;
}
step[k++] = i;
//printf("=====");
//print(a);
//print(r);
} int main(void)
{
int i; while (scanf("%d", &n) != EOF && n)
{
for(i=1; i<=n; i++)
{
scanf("%d", &a[i]);
r[a[i]] = i;
} k = 0;
for (i=n; i>=2; i--)
{
if (r[i] != i)
{
if (r[i] != 1)
flip(r[i]);
flip(i);
}
} printf("%d ", k);
for (i=0; i<k-1; i++)
printf("%d ", step[i]);
printf("%d\n", step[i]); } return 0;
}
/**************************************************************
Problem: 1146
User: liangrx06
Language: C
Result: Accepted
Time:0 ms
Memory:912 kb
****************************************************************/

九度OJ 1146:Flipping Pancake(翻饼子) (递归、游戏)的更多相关文章

  1. 九度OJ 1006:ZOJ问题 (递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:18621 解决:3197 题目描述: 对给定的字符串(只包含'z','o','j'三种字符),判断他是否能AC. 是否AC的规则如下: 1. ...

  2. 九度OJ 1122:吃糖果 (递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1522 解决:1200 题目描述: 名名的妈妈从外地出差回来,带了一盒好吃又精美的巧克力给名名(盒内共有 N 块巧克力,20 > N ...

  3. 九度OJ 1084:整数拆分 (递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2274 解决:914 题目描述: 一个整数总可以拆分为2的幂的和,例如: 7=1+2+4 7=1+2+2+2 7=1+1+1+4 7=1+1 ...

  4. 九度OJ,题目1089:数字反转

    题目描述: 12翻一下是21,34翻一下是43,12+34是46,46翻一下是64,现在又任意两个正整数,问他们两个数反转的和是否等于两个数的和的反转. 输入: 第一行一个正整数表示测试数据的个数n. ...

  5. 九度oj 题目1087:约数的个数

    题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...

  6. 九度OJ 1502 最大值最小化(JAVA)

    题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...

  7. 九度OJ 1500 出操队形 -- 动态规划(最长上升子序列)

    题目地址:http://ac.jobdu.com/problem.php?pid=1500 题目描述: 在读高中的时候,每天早上学校都要组织全校的师生进行跑步来锻炼身体,每当出操令吹响时,大家就开始往 ...

  8. 九度OJ 1531 货币面值(网易游戏2013年校园招聘笔试题) -- 动态规划

    题目地址:http://ac.jobdu.com/problem.php?pid=1531 题目描述: 小虎是游戏中的一个国王,在他管理的国家中发行了很多不同面额的纸币,用这些纸币进行任意的组合可以在 ...

  9. 九度OJ 1024 畅通工程 -- 并查集、贪心算法(最小生成树)

    题目地址:http://ac.jobdu.com/problem.php?pid=1024 题目描述:     省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但 ...

随机推荐

  1. 洛谷 P1478 陶陶摘苹果(升级版)【贪心/结构体排序/可用01背包待补】

    [链接]:https://www.luogu.org/problemnew/show/P1478 题目描述 又是一年秋季时,陶陶家的苹果树结了n个果子.陶陶又跑去摘苹果,这次她有一个a公分的椅子.当他 ...

  2. NewCode

    1.[数论]给你N,求不大于N的最大完全平方数. #include<bits/stdc++.h> #define FOR(i,a,b) for(int i=(a),_b=(b);i< ...

  3. Break Number --AtCoder

    题目描述 Takahashi loves numbers divisible by 2.You are given a positive integer N. Among the integers b ...

  4. js 值类型和引用类型

    function chainStore() { var store1='Nike China'; var store2=store1; store1='Nike U.S.A.'; alert(stor ...

  5. SQLite复杂表的更新方式

    SQLite复杂表的更新方式   在SQLite中,如果早期设计的表无法满足需要,就需要对表进行更新,如修改名字.添加列.如果针对简单表,修改起来相对容易,直接使用提供的ALTER命令即可.但是如果该 ...

  6. Web模糊测试工具Powerfuzzer

    Web模糊测试工具Powerfuzzer   Powerfuzzer是Kali Linux自带的一款Web模糊测试工具.该工具基于各种开源模糊测试工具构建,集成了大量安全信息.该工具高度智能化,它能根 ...

  7. BeanFactory和ApplicationContext的异同

    相同: Spring提供了两种不同的IOC 容器,一个是BeanFactory,另外一个是ApplicationContext,它们都是Java interface,ApplicationContex ...

  8. php 技术知识点汇总

    consul : 服务发现 做服务发现的框架常用的有 zookeeper eureka etcd consul zookeeper,  php中的libzookeeper PHP使用 swagger ...

  9. html特殊字符编码问题导致的细节问题

    今天在写前端html时,一个a标签的链接地址,由于链接地址需要给后台传参数,因此带了部分url参数: 在html源码里写的连接地址是: http://域名/bidder/noticesearch?no ...

  10. 红米note3刷安卓原生

    http://www.romzj.com/rom/63404.htm#comments-version 然后在系统设置里升级系统, http://www.lineageosdownloads.com/ ...