码迷,mamicode.com
首页 > 其他好文 > 详细

煎饼堆

时间:2014-08-12 00:53:23      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:style   color   os   io   strong   for   ar   art   

前言

    该题目确实算作一个很基础的题目,但真正去写时却很容易出错,主要在于两点

  1. 煎饼堆翻动时的细节处理

  2. 输入与输出处理

    因此做一记录

题目
 
 Stacks of Flapjacks 

Background

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.

The Problem

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).

The 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.

The 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<stdio.h>
#include<stdlib.h>
#include<string.h>
FILE *in;
FILE *out;

#ifdef DEBUG
#define CIN     in
#define COUT    out
#else
#define CIN     stdin
#define COUT    stdout
#endif
#define CLR(vec) memset(vec, 0, sizeof(vec))
#define MAXN 35
int pancake[MAXN];
int pancakeCnt;
int start, end;
void reverse(int *vec, int start, int end){
 int tmp;
 while(start < end){
  tmp = vec[start];
  vec[start] = vec[end];
  vec[end]   = tmp;
  start++;
  end--;
 }
}
int searchMax(int *vec, int start, int end){
 int pos;
 int max_pos = start;
 for(pos = start; pos <= end; pos++){
  if(vec[pos] > vec[max_pos]){
   max_pos = pos;
  }
 }
 return max_pos;
}

void solve(int * vec, int start, int end)
{
 int max_pos;
 while(start != end){
  max_pos = searchMax(vec, start, end);
  
  if(vec[max_pos] == vec[start])
   max_pos = start;
  else if(vec[max_pos] == vec[end])
   max_pos = end;
  else
   ;
  if(max_pos == start){
  }else if(max_pos == end){
   printf("%d ", start);
   reverse(vec, start, end);
  }else{
   printf("%d ", max_pos);
   reverse(vec, max_pos, end);
   printf("%d ", start);
   reverse(vec, start, end);
  }
  start++;
 }
 printf("0\n");
}
int main(void){
 int num = 0;
 int val;
 int opt;
 int pos;
#ifdef DEBUG
 in =  freopen("./in",  "r", stdin);
 if(!in){
  fprintf(stderr, "open input file fail\n");
  return 0;
 }
 out =  freopen("./out", "w", stdout);
 if(!out){
  fprintf(stderr, "open input file fail\n");
  return 0;
 }
#endif
 CLR(pancake);
 pancakeCnt = 0;
 while( 2 == scanf("%d%c", &val, &opt)){
  pancake[++pancakeCnt] = val;
  if( ‘\n‘ == (char)opt){
   for(pos = 1; pos < pancakeCnt; pos++){
    printf("%d ", pancake[pos]);
   }
   printf("%d\n", pancake[pancakeCnt]);
   /*init flip*/
   start  = 1, end = pancakeCnt;
   reverse(pancake, start, end);
   solve(pancake, start, end);
   /*clean all*/
   CLR(pancake);
   pancakeCnt = 0;
  }
 }
return 0;
}




煎饼堆,布布扣,bubuko.com

煎饼堆

标签:style   color   os   io   strong   for   ar   art   

原文地址:http://my.oschina.net/u/572632/blog/300180

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!