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

改过的AC精简版(C++)

Posted by majia001 at 2006-08-20 15:40:47 on Problem 2969
In Reply To:这里边的所有测试数据都通过了,还是WA,哪位看下哪错了,给些测试数据! Posted by:194 at 2006-08-20 14:18:14
#include <cstdio>
#include <algorithm>
using namespace std;
char a[1010],c;

sm(){int i,sum=0;for(i=0;a[i];i++)sum+=a[i]-'0';return sum;}
pt(){
	int j,t;
	t=sm();
	if(t%3==0){
		if(t){for(j=strlen(a)-1;~j;j--)printf("%c",a[j]);puts("");}
		else puts("0");
		return 1;
	}else return 0;
}
void pe(){puts("impossible");}
int main(){
	int i,j,k,m,sum,len;
	scanf("%s",a);len=strlen(a);sort(a,a+len);
	if(a[0]=='0'){
		if(pt())return 0;
		for(i=1,sum=sm()%3;a[i]&&(a[i]-'0')%3!=sum;i++);
		if(!a[i]){
			for(;;){
				for(m=1;(a[m]-'0')%3==0;m++);
				for(j=m;a[j];j++)a[j]=a[j+1];
				if(pt())return 0;
			}
		}
		for(j=i;j<len;j++)a[j]=a[j+1];
		pt();return 1;
	}else{
		for(i=0;a[i]&&a[i]!='5';i++);
		if(!a[i]){pe();return 0;}
		c=a[i];a[i]=a[0];a[0]=c;
		sort(a+1,a+len);
		if(pt())return 0;
		for(i=1,sum=sm()%3;a[i]&&(a[i]-'0')%3!=sum;i++);
		if(!a[i]){
			for(m=1,k=0;a[m]&&((a[m]-'0')%3==0||(a[m]-'0')%3==5%3);m++)if((a[m]-'0')%3==5%3)k++;
			if(k==1){pe();return 1;}
			while(1){
				for(m=1;a[m]&&(a[m]-'0')%3==0;m++);
				if(!a[m]){pe();return 0;}
				for(j=m;a[j];j++)a[j]=a[j+1];
				if(pt())return 1;
			}
		}
		for(j=i;j<strlen(a);j++)a[j]=a[j+1];
		pt();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