More actions
imported>Unknown No edit summary |
(Table transclusion repair v1) |
||
| Line 1: | Line 1: | ||
==== 소감 ==== | ==== 소감 ==== | ||
2005/05/10 Accepted 0:00.029 64 | |||
문제의 해법이 바로 보이는 쉬운문제였다. | 문제의 해법이 바로 보이는 쉬운문제였다. | ||
| Line 20: | Line 20: | ||
int main() | int main() | ||
{ | { | ||
int stack | int stack[MAX_SIZE]; | ||
int sortStack | int sortStack[MAX_SIZE]; | ||
int sizeStack; | int sizeStack; | ||
while(1) | while(1) | ||
| Line 39: | Line 39: | ||
for (int i = size - 1; i >= 0; i--) | for (int i = size - 1; i >= 0; i--) | ||
{ | { | ||
if (s | if (s[i] != sort_s[i]) | ||
{ | { | ||
for (int j = 1; j < i; j++) | for (int j = 1; j < i; j++) | ||
{ | { | ||
if (s | if (s[j] == sort_s[i]) | ||
{ | { | ||
flip(s, j); | flip(s, j); | ||
| Line 62: | Line 62: | ||
for (int i = 0; i <= size/2; i++) | for (int i = 0; i <= size/2; i++) | ||
{ | { | ||
SWAP(s | SWAP(s[i], s[size-i], temp); | ||
} | } | ||
} | } | ||
| Line 74: | Line 74: | ||
for (int j = i + 1; j < size; j++) | for (int j = i + 1; j < size; j++) | ||
{ | { | ||
if (s | if (s[min] > s[j]) | ||
min = j; | min = j; | ||
} | } | ||
SWAP(s | SWAP(s[i], s[min], temp); | ||
} | } | ||
} | } | ||
| Line 93: | Line 93: | ||
break; | break; | ||
} | } | ||
cin >> s | cin >> s[i]; | ||
sort_s | sort_s[i] = s[i]; | ||
} | } | ||
return i; | return i; | ||
| Line 102: | Line 102: | ||
{ | { | ||
for (int i = 0; i < size; i++) | for (int i = 0; i < size; i++) | ||
cout << s | cout << s[i] << " "; | ||
cout << endl; | cout << endl; | ||
} | } | ||
---- | ---- | ||
[[AOI]] [[StacksOfFlapjacks]] | [[AOI]] [[StacksOfFlapjacks]] | ||
Latest revision as of 12:46, 27 March 2026
소감
2005/05/10 Accepted 0:00.029 64
문제의 해법이 바로 보이는 쉬운문제였다.
소스
// 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;
}