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

哪位给看一下,改了好多还是WA!快崩溃了!给几组测试数据!

Posted by 194 at 2006-08-18 08:52:03 on Problem 2969
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	char a[1010],c;
	int i,j,m,len,sum;
	cin>>a;
	len=strlen(a);
	sort(a,a+len);
	if(a[0]=='0')
	{
		i=0;
		sum=0;
		while(i<len)
		{
			sum=sum+(int)(a[i]-'0');
			i++;
		}
		if(sum%3==0)  
		{
			if(sum!=0)
			{
				for(j=strlen(a)-1;j>=0;j--)
				    cout<<a[j];
			    cout<<endl;
			    return 1;
			}
			else 
			{
				cout<<"0"<<endl;
				return 1;
			}
		}
		else
		{
			i=1;
			while(a[i]%3!=sum%3)
			{
				i++;
				if(i==len) 
				{
					m=1;
					while(1)
					{
						while(a[m]=='0')
							m++;
						for(j=m;j<strlen(a);j++)
						    a[j]=a[j+1];
						i=0;
	                	sum=0;
                		while(i<strlen(a))
						{
	                		sum=sum+(int)(a[i]-'0');
	                		i++;
						}
						if(sum%3==0)
						{
							if(sum==0)
							{
								cout<<"0"<<endl;
								return 1;
							}
							for(j=strlen(a)-1;j>=0;j--)
		             		    cout<<a[j];
		            	    cout<<endl;
	             		    return 1;
						}
					}
				}
			}
			for(j=i;j<len;j++)
				a[j]=a[j+1];
			i=0;
			sum=0;
			while(i<strlen(a))
			{
				sum=sum+(int)(a[i]-'0');
				i++;
			}
			if(sum==0)  
			{
				cout<<"0"<<endl;
				return 1;
			}
			for(j=strlen(a)-1;j>=0;j--)
				cout<<a[j];
			cout<<endl;
			return 1;
		}
		
	}
	else
	{
		i=0;
		while(a[i]!='5')
		{
			i++;
			if(i==len)
			{
				cout<<"impossible"<<endl;
				return 0;
			}
		}
		if(i!=0)
		{
			c=a[i];
			a[i]=a[0];
			a[0]=c;
		}
		sort(a+1,a+len);
		i=0;
		sum=0;
		while(i<len)
		{
			sum=sum+(int)(a[i]-'0');
			i++;
		}
		if(sum%3==0)  
		{
			for(j=strlen(a)-1;j>=0;j--)
				cout<<a[j];
			cout<<endl;
			return 1;
		}
		else
		{
			i=1;
			while(a[i]%3!=sum%3)
			{
				i++;
				if(i==len) 
				{
					while(strcmp(a,"5"))
					{
						for(j=1;j<strlen(a);j++)
						    a[j]=a[j+1];
						i=0;
	                	sum=0;
                		while(i<strlen(a))
						{
	                		sum=sum+(int)(a[i]-'0');
	                		i++;
						}
						if(sum%3==0)
						{
							for(j=strlen(a)-1;j>=0;j--)
		             		    cout<<a[j];
		            	    cout<<endl;
	             		    return 1;
						}
					}
					cout<<"impossible"<<endl;
					return 1;
				}
			}
			for(j=i;j<len;j++)
				a[j]=a[j+1];
			for(j=strlen(a)-1;j>=0;j--)
				cout<<a[j];
			cout<<endl;
			return 1;
		}
	}

}

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