uva 120 stacks of flapjacks

小水题一个~

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;

string str;

typedef struct nod{
	int d;
	int rate;
}nod;

nod node[500];

void flip(int bottom, int top)
{
	nod temp[3000];
	int t = 1;
	for(int i = top; i <= bottom; i++)
	{
		temp[t].d = node[i].d;
		temp[t++].rate = node[i].rate;
	}
	t--;
	for(int i = top; i <= bottom; i++)
	{
		node[i].d = temp[t].d;
		node[i].rate = temp[t--].rate;
	}
}

int main()
{
	//freopen("ztest.txt","r",stdin);
	//freopen("zans.txt","w",stdout);
	while(getline(cin, str))
	{
		
		int u = 1;
		int a[4000];
		int k = 0; 
		for(int i = 0; i < str.size(); i++)
		{
			if(str[i] == ' ' )
			{
				a[u] = k;
				node[u++].d = k;
				k = 0;
			}
			else
			{
				k = k*10+str[i]-'0';
			}
		}
		a[u] = k;
		node[u++].d = k;
		u --;
		for(int i = 1; i <= u ;i++)
		{
			printf("%d",node[i].d);
			if(i != u)
				printf(" ");
		}
		printf("\n");
		sort(a+1, a+1+u);
		for(int i = 1; i <= u; i++)
		for(int j = 1; j <= u; j++)
		{
			if(node[i].d == a[j])
			{
				node[i].rate = j;
				break;
			}
		}
		for(int i = u; i >= 1; i--)
		{
			for(int j = 1; j <= u; j++)
			{
				if(i == node[j].rate)
				{
					 if(node[j].rate == j)
					 	break;
					else
					{
						flip(j, 1);
						if(j != 1)
							printf("%d ",u-j+1);
						flip(i, 1);
						if(i != 1)
							printf("%d ",u-i+1);
						break;
					}
				}
			}
		}
		printf("0\n");
	}
	return 0;
}
			
		
				
			
				
				
				
				
				
			 

猜你喜欢

转载自blog.csdn.net/wukongakk/article/details/80992018
120