can anybody help me (120)(stack of flapjack)?
Posted: Mon Feb 05, 2007 6:04 am
removed the code
Code: Select all
1 9 33 4 45 2 78 100
Code: Select all
1 9 33 4 45 2 78 100
4 3 5 4 6 0
Code: Select all
1 9 33 4 45 2 78 100
7 2 3 5 4 7 0
Code: Select all
//Question http://acm.uva.es/p/v1/120.html
//CPU: 0.002, Memory: Minimum
//state: Runtime Error (Signal 11)
#include <iostream>
#include <cstring>
using namespace std;
void sortPancake(int *r, int n) //array R have 5 element
{
for(int j=1; j<n; j++) //Insertion Sort
{
int k = r[j];
int i = j-1;
while(i>=0 && r[i]>k){
r[i+1] = r[i];
i--;
}
r[i+1] = k;
}
}
void flip(int *r, int n)
{
for(int i=0; i<=n/2; i++)
{
int temp = r[i];
r[i] = r[n-i];
r[n-i] = temp;
}
}
int main()
{
int p[30]={0}, count=0, r[30]={0}, input;
while(cin >> input)
{
p[count++] = r[count] = input;
if(cin.eof())
break;
}
for(int i=0; i<count; i++)
cout << p[i] << " ";
cout << "\n";
sortPancake(r, count); //Insertion Sort
for(int i=count-1; i>=0; i--)
{
if(p[i]==r[i])
continue;
if(p[0]!=r[i])
{
int position = 0;
while(p[position]!=r[i])
position++;
flip(p, position);
cout << count-position << " ";
}
flip(p, i); //p[0]==r[i]
cout << count-i << " ";
}
cout << "0";
//system("pause");
return (0);
}
Code: Select all
if(cin.eof()) break;
Code: Select all
2
33 21 45
33 55
1
2 1
1 2
12 45 1 2 4 7 5 11 21 99 12 47
56 12 48
75 2 1 44 53 84 21 46 82 43 99 49 89 7 9
1 2 3 6 7 8 4 5
1 3 19 4 95 62 42 43 13 2
1 2 3 4 5 6 7 8 9 10 11 12 14 13 15 16 17 18 19 20
Code: Select all
2
0
33 21 45
1 1 0
33 55
0
1
0
2 1
1 0
1 2
0
12 45 1 2 4 7 5 11 21 99 12 47
3 1 2 11 3 11 4 6 6 7 7 11 8 0
56 12 48
1 2 0
75 2 1 44 53 84 21 46 82 43 99 49 89 7 9
5 1 13 2 10 3 11 4 5 9 6 11 7 14 8 13 9 12 10 13 11 12 13 13 0
1 2 3 6 7 8 4 5
3 1 6 2 5 3 7 4 0
1 3 19 4 95 62 42 43 13 2
6 1 6 2 4 3 4 8 5 6 8 7 9 8 0
1 2 3 4 5 6 7 8 9 10 11 12 14 13 15 16 17 18 19 20
1 1 2 3 3 4 5 5 6 8 7 8 0
all pancakes separated by a space.
Sedefcho wrote:
INPUT:Code: Select all
... 4 4 1 4 4 5 4 4 1 4 4 5 4 99 7 99 0 7 99 0 2 4 4 4 2 4 11 13 15 17 13 15 11 20 20 200 20 20 200 20 20
Code: Select all
8 4 6 7 5 2
1 2 3 5 4
7 3 5 4 8 9 10 1 13
2
33 21 45
33 55
1
2 1
1 2
56 12 48
75 2 1 44 53 84 21 46 82 43 99 49 89 7 9
1 2 3 6 7 8 4 5
1 3 19 4 95 62 42 43 13 2
1 2 3 4 5 6 7 8 9 10 11 12 14 13 15 16 17 18 19 20
Code: Select all
8 4 6 7 5 2
1 4 2 5 3 4 5 0
1 2 3 5 4
2 1 2 3 0
7 3 5 4 8 9 10 1 13
3 2 8 5 8 6 0
2
0
33 21 45
2 0
33 55
0
1
0
2 1
1 0
1 2
0
56 12 48
1 2 0
75 2 1 44 53 84 21 46 82 43 99 49 89 7 9
5 1 13 2 11 3 6 4 10 5 14 6 7 10 8 12 9 11 14 12 14 0
1 2 3 6 7 8 4 5
3 1 4 6 0
1 3 19 4 95 62 42 43 13 2
6 1 6 2 4 3 4 6 5 9 6 8 9 0
1 2 3 4 5 6 7 8 9 10 11 12 14 13 15 16 17 18 19 20
8 7 8 9 0
Code: Select all
#include<iostream>
#include<sstream>
#include<stack>
using namespace std;
int min(int a[],int i,int n)
{
int m=i;
for(int j=i;j<n;j++)
if(a[m]>a[j])m=j;
return m;
}
void flip(int a[],int ind,int n)
{
stack<int> sta;
for(int i=ind;i<n;i++)
sta.push(a[i]);
for(int i=ind;i<n;i++)
{
a[i]=sta.top();
sta.pop();
}
}
int main()
{
int sta[100];
char s[300];
while(cin.getline(s,300))
{
stringstream sin(s);
int i=0;
while(sin>>sta[i++])cout<<sta[i-1]<<" ";
int n=i-1;
cout<<"\n";
for(i=0;i<n-1;i++)
{
int ind=min(sta,i,n);
if(ind!=i)
{
if(ind<n-1)
{
cout<<ind+1<<" ";
flip(sta,ind,n);
}
cout<<i+1<<" ";
flip(sta,i,n);
}
}
cout<<"0\n";
}
return 0;
}