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

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

Posted by 13091264 at 2010-11-13 16:30:38 on Problem 1208
In Reply To:求高人给看看代码,好几天了,纠结中,实在不知道错在哪了,刚刚学的编程,见谅啊,代码有点乱!! Posted by:13091264 at 2010-11-13 14:46:10
不用麻烦了,小弟写了个数据生成器,找到问题了,都是因为平时的编程习惯不好,下面是AC了的代码
#include<iostream>
#include<algorithm>
#include <stack>
using namespace std;
  char ch;
  int type,i,n,t1,t2,k,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)
	{
		k=-1;
		t1=t2=0;
		if(ch=='m')
			while(1)
			{
				ch=getchar();	
				if(ch>='0'&&ch<='9')
				{
					if(k==-1)
					 t1=10*t1+(ch-'0');
					else t2=10*t2+(ch-'0');
				}
				if(ch=='t')
                { 
                    type=1;k++;
                }
				if(ch=='r')
                {
                    type=2;k++;
                }
				if(ch=='\n') break;			
			}
		if(ch=='p')
			while(1)
			{
				ch=getchar();
			if(ch>='0'&&ch<='9')
				{
					if(k==-1)
					 t1=10*t1+(ch-'0');
					else t2=10*t2+(ch-'0');
				}
				if(ch=='t')
                { 
                    type=3;k++;
                }
				if(ch=='r')
                {
                    type=4;k++;
                }
				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<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