U E D R , A S I H C RSS

Stacks Of Flapjacks/문보창

소감

{{| 2005/05/10 Accepted 0:00.029 64 |}}

문제의 해법이 바로 보이는 쉬운문제였다.

소스

~cpp 
// no 120 - Stacks of Flapjacks
#include <iostream>
using namespace std;

#define SWAP(x, y, t) ((t) = (x), (x) = (y), (y) = (t))
#define MAX_SIZE 30

int input_stack(int * s, int * sort_s);
void show_stack(int * s, int size);
void sort_stack(int * s, int size);
void flip_cake(int * s, int * sort_s, int size);
void flip(int * s, int size);

int main()
{
	int stack[MAX_SIZE];
	int sortStack[MAX_SIZE];
	int sizeStack;
	while(1)
	{
		sizeStack = input_stack(stack, sortStack);
		if (sizeStack == -1)
			break;
		show_stack(stack, sizeStack);
		sort_stack(sortStack, sizeStack);
		flip_cake(stack, sortStack, sizeStack);
	}
	return 0;
}

void flip_cake(int * s, int * sort_s, int size)
{
	for (int i = size - 1; i >= 0; i--)
	{
		if (s[i] != sort_s[i])
		{
			for (int j = 1; j < i; j++)
			{
				if (s[j] == sort_s[i])
				{
					flip(s, j);
					cout << size - j << " ";
					break;				
				}
			}
			flip(s, i);
			cout << size - i << " ";
		}
	}
	cout << 0 << endl;
}

void flip(int * s, int size)
{
	int temp;
	for (int i = 0; i <= size/2; i++)
	{
		SWAP(s[i], s[size-i], temp);
	}
}

void sort_stack(int * s, int size)
{
	int min, temp;
	for (int i = 0; i < size; i++)
	{
		min = i;
		for (int j = i + 1; j < size; j++)
		{
			if (s[min] > s[j])
				min = j;
		}
		SWAP(s[i], s[min], temp);
	}
}

int input_stack(int * s, int * sort_s)
{
	int i;
	if (cin.peek() == EOF)
		return -1;
	for (i = 0; i < MAX_SIZE; i++)
	{
		if (cin.peek() == '\n')
		{
			cin.get();
			break;
		}
		cin >> s[i];
		sort_s[i] = s[i];
	}
	return i;
}

void show_stack(int * s, int size)
{
	for (int i = 0; i < size; i++)
		cout << s[i] << " ";
	cout << endl;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:28:07
Processing time 0.0137 sec