uva 120(排序,检索)

2023-10-26

题目:

Stacks and Queues are often considered the bread and butter of data structures and find use in architecture, parsing, operating systems, and discrete event simulation. Stacks are also important in the theory of formal languages.

This problem involves both butter and sustenance in the form of pancakes rather than bread in addition to a finicky server who flips pancakes according to a unique, but complete set of rules.

Given a stack of pancakes, you are to write a program that indicates how the stack can be sorted so that the largest pancake is on the bottom and the smallest pancake is on the top. The size of a pancake is given by the pancake's diameter. All pancakes in a stack have different diameters.

Sorting a stack is done by a sequence of pancake ``flips''. A flip consists of inserting a spatula between two pancakes in a stack and flipping (reversing) the pancakes on the spatula (reversing the sub-stack). A flip is specified by giving the position of the pancake on the bottom of the sub-stack to be flipped (relative to the whole stack). The pancake on the bottom of the whole stack has position 1 and the pancake on the top of a stack of n pancakes has position n.

A stack is specified by giving the diameter of each pancake in the stack in the order in which the pancakes appear.

For example, consider the three stacks of pancakes below (in which pancake 8 is the top-most pancake of the left stack):

         8           7           2
         4           6           5
         6           4           8
         7           8           4
         5           5           6
         2           2           7

The stack on the left can be transformed to the stack in the middle via flip(3). The middle stack can be transformed into the right stack via the command flip(1).

input

The input consists of a sequence of stacks of pancakes. Each stack will consist of between 1 and 30 pancakes and each pancake will have an integer diameter between 1 and 100. The input is terminated by end-of-file. Each stack is given as a single line of input with the top pancake on a stack appearing first on a line, the bottom pancake appearing last, and all pancakes separated by a space.

output

For each stack of pancakes, the output should echo the original stack on one line, followed by some sequence of flips that results in the stack of pancakes being sorted so that the largest diameter pancake is on the bottom and the smallest on top. For each stack the sequence of flips should be terminated by a 0 (indicating no more flips necessary). Once a stack is sorted, no more flips should be made.

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 <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
const int M = 40;
int main() {
	int in[M], n = 0, flag, temp[M], temp1[M];
	char c;
	while(scanf("%d%c", &in[n++], &c) != EOF) {
		if (c == '\n') {
			flag = 0;
			for (int i = 0; i < n - 1; i++)
				printf("%d ", in[i]);
			printf("%d\n", in[n - 1]);
			for (int i = 0; i < n; i++) 
				temp[i] = in[i]; sort(in, in + n);
			int k = n, pos = 0, max;
			while (flag = 1) {
				int i, j;
				for (i = 0; i < n; i++)
					if (in[i] != temp[i])
						break;
				if (i == n) {
					flag = 1;
					printf("0\n");
					break;
				}
				max = temp[0], pos = 0;
				for (i = 1; i < k; i++)//先找到最大值的位置
					if (temp[i] > max) {
						max = temp[i];
						pos = i;
					}
				if (pos == k - 1)//如果已经在最后了就让k--最大值变成比他小一个的数
					k--;
				else if (pos == 0) {//如果最大值在开头,就把最大的值放到最后
					printf("%d ", n - k + 1);
					for (i = 0, j = k - 1; i < k, j >= 0; i++, j--)
						temp1[i] = temp[j];
					for (j = 0; j < k; j++) 
						temp[j] = temp1[j];
					k--;
				}
				else {
					printf("%d ", n - pos);
					for (i = pos, j = 0; i >= 0, j <= pos; i--, j++)//把最大的值放到开头
						temp1[j] = temp[i];
					for (j = 0; j <= pos; j++)
						temp[j] = temp1[j];
				}
			}
			n = 0;
		}	 
	}
	return 0;
}


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

uva 120(排序,检索) 的相关文章

  • C++中sort函数详解

    原文链接点这 0 简介 sort函数用于C 中 对给定区间所有元素进行排序 默认为升序 也可进行降序排序 sort函数进行排序的时间复杂度为n log2n 比冒泡之类的排序算法效率要高 sort函数包含在头文件为 include的c 标准库
  • shell排序 C++

    Shell排序算法严格来说基于插入排序的思想 又称为希尔排序或缩小增量排序 Shell排序算 法的排序流程如下 1 将有 n个元素的数组分成n 2 个数字序列 第 1 个数据和第n 2 1 个数据为一对 2 一次循环使每一个序列对排好顺序
  • 如何确定一个期刊是不是EI?

    去爱思唯尔官网下载最新的目录 网址 https www elsevier com solutions engineering village content compendex 打开EXCEL查看 SERIALS就是罗列出的所有的EI期刊和
  • UVA 10010 - Where's Waldorf? 题解

    Time limit 3 000 seconds Where s Waldorf Given a m by n grid of letters and a list of words find the location in the gri
  • C语言归并排序算法

    今天我要和大家分享的是一个排序算法 归并算法 如果说快速排序是让一个数组分成很多小集体进行排序 那么归并排序就是让很多有序的小集体合成一个有序数组 思路 如果是升序 对于每一个数字来说其本身是有序的 最初让两个只有一个元素的数组arr1 a
  • 【每日一题】排序子序列(贪心)

    题目来源 牛客网 链接 排序子序列 题目描述 牛牛定义排序子序列为一个数组中一段连续的子序列 并且这段子序列是非递增或者非递减排序的 牛牛有一个长度为n的整数数组A 他现在有一个任务是把数组A分为若干段排序子序列 牛牛想知道他最少可以把这个
  • Python实现选择排序

    选择排序简介 选择排序 Selection sort 是一种简单直观的排序算法 简单来说就是从无序队列里面挑选最小的元素 和无序队列头部元素替换 放到有序队列中 最终全部元素形成一个有序的队列 选择排序原理 首先在未排序序列中找到最小 大
  • CH8-排序

    文章目录 1 基本概念和排序方法概述 1 1 排序方法的分类 1 2 存储结构 顺序表 2 插入排序 2 1 插入排序的种类 直接插入 折半插入 希尔排序 3 交换排序 3 1 冒泡排序 3 2 快速排序 4 选择排序 4 1 直接排序 4
  • linux--shell错误:syntax error near unexpected token ‘('

    这几天编写了几个简单的shell程序 然后都出现了syntax error near unexpected token 的错误 然后实在是检查不出错误 后面百度了才找到的原因 之前错误的程序片段如下 usr whoami dr pwd 提示
  • 基数排序(C语言)

    基数排序 基数排序 Radix sort 是一种非比较型的排序算法 最早用于解决卡片排序的问题 它的工作原理是将待排序的元素拆分为k个关键字 其中k为最大值的位数 从低位开始进行稳定排序 注意 数列中的元素都是非负整数 基数排序是一种稳定的
  • C语言:合并两个有序数列,并保持有序性。

    C语言 合并两个有序数组 并保持有序性 include
  • 为什么软件开发很难?真相了

    为什么软件开发很难 真相了 作者 Jeremy Mikkol 本文认为这种困难与编程语言无关 因为现代的编程语言已经足够好了 那么 原因到底是什么 有一种观点认为 使用更好的编程语言就会让软件开发变得更容易 更高效 在汇编或 Fortran
  • Vector的自动排序Sort

    建立了一个结构体 然后用容器进行存放 想对其进行排序 vector支持sort函数 但是需要自己指定排序函数 方法如下 1 需要包含头文件 include
  • 交换和--排序

    LeetCode 面试题 16 21 交换和 给定两个整数数组 请交换一对数值 每个数组中取一个数值 使得两个数组所有元素的和相等 返回一个数组 第一个元素是第一个数组中要交换的元素 第二个元素是第二个数组中要交换的元素 若有多个答案 返回
  • C语言实现快速排序与归并排序

    快排 代码如下 include
  • UVA-11059 最大乘积 题解答案代码 算法竞赛入门经典第二版

    GitHub jzplp aoapc UVA Answer 算法竞赛入门经典 例题和习题答案 刘汝佳 第二版 数据量不大 暴力即可 include
  • 力扣每日一题——三角形的最大周长

    题目链接 class Solution public int largestPerimeter vector
  • UVa 1347 Tour

    题目 Tour 题意 来自luogu John Doe想用最小的路程游览完所有目的地 每个目的地都用坐标xi yi表示 任何两目的地的xi都不相同 两目的地之间的路程是两点之间的直线距离 John是这样走的 他从最左边的点开始 然后只能向右
  • js 对数组对象进行排序

    let listData id 1 name 测试1 presenttime 1557883600000 id 2 name 测试2 presenttime 1580751813000 id 3 presenttime 1561448381
  • List sort comparator按照自定义规则排序

    statusList I R N return N R I public String handStateGroup List

随机推荐

  • 【转】虚拟机网络服务启动失败Failed to start LSB 解决方法

    场景 克隆了一个虚拟机后不能重启它的网络服务编辑IP配置文件 vi etc sysconfig network scripts ifcfg ens33重新修改了ip后 发现还是报错如下 错误信息 Failed to start LSB 网络
  • java中根据某一属性比较两个list集合是否相同

    创建两个示例列表 List
  • 二叉树前中后序遍历非递归实现

    前序遍历 public static void prifixOrder TreeNode root System out print 前序遍历 Stack
  • XSS-9注入靶场闯关(小游戏)——第九关

    1 这个关卡与第八关相同 直接把编码放上去尝试 之前的payload也无法使用 106 97 118 97 115 99 114 105 112 116 58 97 108 101 114 116 40 49 41 2 输入一个正常连接查看
  • Subscriber class .NewsFragment and its super classes have no public methods

    使用EventButs3 0 0 出现以下错误 Caused by de greenrobot event EventBusException Subscriber class com gozap beacontower ui NewsFr
  • 【C++初阶】简析拷贝构造、赋值运算符重载

    hello 各位读者大大们你们好呀 系列专栏 C 学习与应用 本篇内容 构造函数的概念与特征 基本使用方法 运算符重载 赋值运算符重载 前置 后置 的使用 作者简介 计算机海洋的新进船长一枚 请多多指教 同期文章 C 初阶 简析构造函数 析
  • 告别代码复制粘贴,傻瓜式提取 PyTorch 中间层特征

    内容导读 特征提取是图像处理过程中常需要用到的一种方法 其效果好坏对模型的泛化能力有至关重要的影响 特征提取 Feature extraction 在机器学习 模式识别和图像处理中应用广泛 它从初始的一组测量数据开始 建构出提供信息且不冗余
  • C# Modbus CRC校验

    Modbus CRC校验 直接输入byte 输出bool public static bool CRC Check byte byteData bool Flag false byte CRC new byte 2 UInt16 wCrc
  • React 深入系列1:React 中的元素、组件、实例和节点

    React 深入系列 深入讲解了React中的重点概念 特性和模式等 旨在帮助大家加深对React的理解 以及在项目中更加灵活地使用React React 中的元素 组件 实例和节点 是React中关系密切的4个概念 也是很容易让React
  • 从0搭建react项目

    一 快速开始 全局安装脚手架 npm install g create react app 复制代码 通过脚手架搭建项目 create react app lt 项目名称 gt 复制代码 开始项目 cd lt 项目名 gt npm run
  • 音频-什么是PCM编码格式?

    PCM中文称脉冲编码调制 Pulse Code Modulation 是70年代末发展起来的 记录媒体之一的CD 在80年代初由飞利浦和索尼公司共同推出 脉码调制的音频格式也被DVD A所采用 它支持立体声和5 1环绕声 1999年由DVD
  • 正则表达式-----小数点后允许有两位数字

    校验是否全由数字组成 function isDigit s var patrn 0 9 1 20 if patrn exec s return false return true 校验登录名 只能输入5 20个以字母开头 可带数字 的字串
  • eclipse关联spring源码

    在Eclipse中如何关联spring framework的文档和源代码 1 到官方网站去下载spring framework的jar包 spring framework jar包的下载地址是 http repo spring io rel
  • 解决ubuntu18.04卡在“starting Gnome Display Manager“

    我提前安装了ssh 所以可以ssh进行下面的操作 如果你没有 你可以进入命令行 或者其他模式进行下面的操作 好多人都遇到这个问题 我提前说下 安装驱动之前 最好安装个ssh ssh 怎么用自己百度吧 这样万一你的电脑卡住了 你找个别人的电脑
  • autoApprove

    服务端最主要的一个配置就是使用 EnableAuthorizationServer 注解 该注解的作用就是引入了一些 OAuth2 相关的端点 包含以下的端点 AuthorizationEndpoint 根据用户认证获得授权码 有下面两个方
  • 工作日记:JavaScript生成随机色

    不多啰啰 直接上硬货 获取指定闭区间的随机数 param min 最小值 param max 最大值 returns number export function getRandomNum min max let result if min
  • 2007.08.21单链表的运算(查找,插入,删除)

    2 查找 1 按序号查找 在单链表中 由于每个结点的存储位置都放在其前一结点的next域中 因而即使知道被访问结点的序号i 也不能像顺序表那样直接按序号i访问一维数组中的相应元素 实现随机存取 而只能从链表的头指针出发 顺链域next诸葛结
  • 代理记账公司怎样找客户?教你一个简单又有效的方法

    针对许多初创期公司而言 资金和人力资源管理都非常欠缺 重心点更偏重于业务流程自身 因而会将会计等有关问题交到代理记账公司 实际上这个是较为聪明的作法 代理记账是指将本公司的财务核算 做账 纳税申报等一系列的工作中所有委派给技术专业做账公司进
  • C语言之求两个整数之和

    include
  • uva 120(排序,检索)

    题目 Stacks and Queues are often considered the bread and butter of data structures and find use in architecture parsing o