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-20 14:18:14 on Problem 2969
#include <iostream>
#include <algorithm>
using namespace std;

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