Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

求高人给看看代码,好几天了,纠结中,实在不知道错在哪了,刚刚学的编程,见谅啊,代码有点乱!!

Posted by 13091264 at 2010-11-13 14:46:10 on Problem 1208
#include<iostream>
#include<algorithm>
#include <stack>
using namespace std;
  char ch;
  int type,i,n,t1,t2,b[50];
  stack <int>a[50];
  stack<int>track;
 void move_onto()
  {
	 if(b[t1]!=b[t2])
	 {
		while(1)
		{ 
          if(a[b[t1]].top()==t1) break;
		   i=a[b[t1]].top();
	       a[i].push(i);
		   a[b[t1]].pop();
		   b[i]=i;
		  
		}
	 
		while(1)
		{ 
          if(a[b[t2]].top()==t2) break;
		   i=a[b[t2]].top();
	       a[i].push(i);
		   a[b[t2]].pop();
		   b[i]=i;
		  
		}
		
		 a[b[t2]].push(t1);
		 a[b[t1]].pop();
          b[t1]=b[t2];
      }
	
}

void move_over()
{ 
   if(b[t1]!=b[t2])
	 {
		  while(1)
		 {
           if(a[b[t1]].top()==t1) break;
		   i=a[b[t1]].top();
	       a[i].push(i);
		   a[b[t1]].pop();
		   b[i]=i;
		   
		 }
		
		 a[b[t2]].push(t1);
		 a[b[t1]].pop();
          b[t1]=b[t2];
     }
  }

void pile_onto()
{
      if(b[t1]!=b[t2])
	 {
		while(1)
		{
          if(a[b[t2]].top()==t2) break;
		   i=a[b[t2]].top();
	       a[i].push(i);
		   a[b[t2]].pop();
		   b[i]=i;
		   
		}
	while(1)
		{
			i=a[b[t1]].top();
			   track.push(i);
			   a[b[t1]].pop();
		    	if(i==t1)
				break;
		}
	while(track.size())
	{
		i=track.top();
		a[b[t2]].push(i);
		b[i]=b[t2];	
       	track.pop();
	}
   }
}


void pile_over()
{
      if(b[t1]!=b[t2])
	 {
	while(1)
		{
			i=a[b[t1]].top();
		    track.push(i);
	        a[b[t1]].pop();
			if(i==t1)
				break;
			}
		
	while(track.size())
	{
		i=track.top();
		a[b[t2]].push(i);
		b[i]=b[t2];
		track.pop();
	}
   }
}


int main()
{
	cin>>n;
	for(i=0;i<n;i++)
	{
	    a[i].push(i);
		b[i]=i;
	}
	
	ch=getchar();
	while(1)
	{
		t1=-1;
		if(ch=='m')
			while(1)
			{
				ch=getchar();	
				if(ch>='0'&&ch<='9')
				{
					if(t1==-1)
					 t1=ch-'0';
					else t2=ch-'0';
				}
				if(ch=='t') type=1;
				if(ch=='r') type=2;
				if(ch=='\n') break;			
			}
		if(ch=='p')
			while(1)
			{
				ch=getchar();
				if(ch>='0'&&ch<='9')
				{
					if(t1==-1)
					 t1=ch-'0';
					else t2=ch-'0';
				}
				if(ch=='t') type=3;
				if(ch=='r') type=4;
				if(ch=='\n') break;
			}
		if(ch=='q') break;
		if(type==1) move_onto();
		if(type==2) move_over();
		if(type==3) pile_onto();
		if(type==4) pile_over();
		ch=getchar();
	}

/*for(i=0;i<10;i++)
cout<<b[i]<<"\t";
cout<<"\n";*/

	for(i=0;i<n;i++)
	{
		printf("%d:",i);
		while(a[i].size())
		{
			track.push(a[i].top());
			a[i].pop();
		}
		while(track.size())
		{
			printf(" %d",track.top());
			track.pop();
		}
		printf("\n");
	}
	system("pause");
	return 0;	
}


Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator