第5周.翻煎饼

[问题描述](老师已经为宝宝们翻译好啦)

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

堆栈和队列通常被认为是数据结构的面包和黄油,可用于体系结构、解析,操作系统和离散事件模拟。堆栈在形式语言理论中也很重要。

 

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

现在的问题涉及黄油和煎饼(而不是面包),同时还有一个根据唯一但完整的规则来翻煎饼的服务器。

 

Given a stack of pancakes, you are to write a programthat indicates how the stack can be sorted so that the largest pancake is on the bottom and thesmallest pancake is on the top.

给你一栈的煎饼,请你编写一个程序用于指示这个栈如何被排序以使得最大的煎饼在最下面而最小的煎饼在最上面。

 

The size of a pancake is given by the pancake’sdiameter.

煎饼的直径将被给出。

 

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 twopancakes in a stack and flipping (reversing) all the pancakes on thespatula (reversing the sub-stack).

一次翻转的意思是:在两个煎饼之间插入铲子,然后将铲子上面的一堆煎饼整体翻过来。也就是指定一个位置,其上的子栈整体翻转。

 

A flip is specified by giving the position of thepancake on the bottom of the sub-stack to be flipped (relative to the whole stack).

翻转的位置将会被给出。

 

The pancake on the bottom of the whole stack hasposition 1

and the pancake on the top of a stack of n pancakeshas position n.

位置是这样定义的:栈底编号为1,栈顶编号为n

 

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

一个栈的煎饼的给出方式,是从上到下给出煎饼的直径。

 

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

举例来说,这是三个栈,左边的栈的最上面的煎饼直径为8

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 stackin the middle via flip(3). The middle stack can be transformed into the right stack via the commandflip(1).

左侧栈,可在位置3(即直径7)处翻转,得到中间的那个栈,而中间那个栈可在位置1(即直径2)处翻转,得到右侧的栈。

 

Input(输入)

The input consists of a sequence of stacks ofpancakes. Each stack will consist of between 1 and 30 pancakes and each pancake will have an integerdiameter between 1 and 100. The input is terminated by end-of-file. Each stack is given as a single lineof 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.

输入由多个煎饼栈组成。每个栈有1到30个煎饼,每个煎饼的直径在1-100之间。以文档结束为输入结束。每个栈,独占一行,从左到右依次代表从栈顶到栈底的煎饼的直径,空格隔开。

 

Output(输出)

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

对于每个煎饼栈,输出首先应原样将栈的数据打印成一行。

随后的一行是翻转位置的次序,空格隔开,以0结束。(结束的目标是最大直径在最下面,最小直径在最上面)。

 

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

 

 */

 

解题思路:

在这里使用java解决问题的,先考虑主要操作,主要操作有两个:

  • 寻找关键点
  • 翻转

我们定义数组里边的最大值和最小值为关键点,找最大最小值就直接遍历一遍数组,选择最大最小值;首先对最大值做处理,题目要求需要把最大值放在栈底,在数组中就是数组的最后边,这里的最大值位置分两种情况:

  • 最大值为数组[0]的位置
  • 最大值在别的位置

 最大值在别的位置时需要先做一次翻转,把最大值放在数组首位,接下来将位于数组首位的最大值翻转至数组末位。至此完成了把最大值放到数组末位的操作;接下来只需要找到最小值位置,进行翻转,将最小值放置数组首位即可。

package Week;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class W_5 {
	private static final int COLUMN_COUNT = 5;
	public static void main(String[] args){
        //读取file中数据存到数组中
        System.out.println("获取数据:");
        String pathname1 = "D:\\Java\\WorkSpaces\\LQB\\src\\Week\\data.txt"; // 绝对路径或相对路径都可以,这里是绝对路径,写入文件时演示相对路径
        File file = new File(pathname1); // 要读取以上路径的input。txt文件
        int[][] datas=null;
        int max,index,min;
        String res="";
        try {
			datas=intFileTOData(file);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        for(int i=0;i<datas.length;i++){
        	int[] test=new int[COLUMN_COUNT];
        	test=datas[i];
        	max=min=test[0];
        	index=0;
        	res="";//存储结果
        	for(int j=0;j<test.length;j++){	//打印,找最大,最大值位置和最小值
        		System.out.print(test[j]+" ");
        		if(max<test[j]){
        			max=test[j];
        			index=j;
        		}
        		if(min>test[j]) min=test[j];
        	}
        	System.out.println();
        	
        	if(index!=(test.length-1)&&index!=0){
        		res=res+(index+1)+" ";
        		//翻转数组
        		test=transtrate(test,index);
        	}else if(index==0){
        		res+="1 ";
        		test=transtrate(test,COLUMN_COUNT-1);
        	}
        		
        	for(int j=0;j<test.length;j++){
        		if(test[j]==min&&j!=0){
        			res+=(COLUMN_COUNT-j+" ");
        			break;
        		}
        	}
        	res=res+"0";
        	System.out.println(res);
        }
        
	}
	private static int[] transtrate(int[] test, int index) {
		// TODO Auto-generated method stub
		int[] temp=new int[test.length];
		for(int i=0;i<=index;i++){
			temp[i]=test[index-i];
		}
		for(int i=index+1;i<test.length;i++){
			temp[i]=test[i];
		}
		return temp;
	}
	public static int[][] intFileTOData(File file) throws Exception {
        /* 读入TXT文件 */
        // String pathname = "trainData.txt"; //
        // 绝对路径或相对路径都可以,这里是绝对路径,写入文件时演示相对路径
        // File filename = new File(pathname); // 要读取以上路径的input。txt文件
        InputStreamReader reader = new InputStreamReader(new FileInputStream(file)); // 建立一个输入流对象reader
	    BufferedReader br = new BufferedReader(reader); // 建立一个对象,它把文件内容转成计算机能读懂的语言
	    int[][] doubleArray = null;
	    try{
			String line = null;
	        StringBuffer sb = new StringBuffer();
	        while ((line = br.readLine()) != null) {
	            sb.append(line).append("\n");
	        }
	        System.out.println("=============转一维数组================");
	        String[] singleArray = sb.toString().split("\\s+");
	        // 遍历一维数组
	        for (String str1 : singleArray) {
	            System.out.println(str1);
	        }
	
	        System.out.println("\n=============转二维数组================");
	        int rows = singleArray.length / COLUMN_COUNT;// 数组行数
	        int num = -1;
	        doubleArray = new int[rows][COLUMN_COUNT];
	        for (int i = 0; i < rows; i++) {
	            for (int j = 0; j < COLUMN_COUNT; j++) {
	                doubleArray[i][j] =Integer.parseInt(singleArray[++num]);
	            }
	        }
	        // 遍历二维数组
	        for (int i = 0; i < rows; i++) {
	            for (int j = 0; j < COLUMN_COUNT; j++) {
	                System.out.print(doubleArray[i][j] + "\t");
	            }
	            System.out.println();
	        }
	    } catch (FileNotFoundException e) {
	        e.printStackTrace();
	    } catch (IOException e) {
	        e.printStackTrace();
	    } finally {
	        if (br != null) {
	            try {
	                br.close();
	                br = null;
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }
	    }
        return doubleArray;
    }
}

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值