-
Notifications
You must be signed in to change notification settings - Fork 0
/
uva120.cpp
68 lines (63 loc) · 1.14 KB
/
uva120.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
void flip(int *pancakes,int p)
{
int s=0;
while(s<p)
{
int temp = pancakes[s];
pancakes[s++] = pancakes[p];
pancakes[p--] = temp;
}
}
void sortPancake(int *pancakes,int psize)
{
for(int i=psize-1;i>=0;i--)
{
int maxPos = i;
for(int j=i-1;j>=0;j--)
{
if(pancakes[j] > pancakes[maxPos])
maxPos = j;
}
if(maxPos < i)
{
if(maxPos>0)
{
printf("%i ",psize-maxPos);
flip(pancakes,maxPos);
}
printf("%i ",psize-i);
flip(pancakes,i);
}
}
printf("0\n");
}
int pancakes[30];
char buffer[1000];
int main()
{
while(!cin.eof())
{
cin.getline(buffer,1000);
int i=0,sz=0,num=0;
if(buffer[0] == '\0')break;
while(buffer[i]==' ' ||(buffer[i]>='0' && buffer[i]<='9') )
{
if(buffer[i] == ' ')
{
pancakes[sz++] = num;
num = 0;
while(buffer[i] == ' ') i++;
}else{
num = num*10 + (buffer[i]-'0');
i++;
}
}
pancakes[sz++] = num;
printf("%s\n",buffer);
sortPancake(pancakes,sz);
}
}