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

Help! I got WA but I don't know why....

Posted by Accepted at 2006-08-15 13:35:43 on Problem 2969
#include<stdio.h>
char str[1010];
int a[1010];
int len;

int cmp(const void *a,const void *b)
{
    return *((int *)a)-*((int *)b);
}

int solve(int rem)
{
    int i,j;
    for(i=2;i<=len;i++){
        if(a[i]%3==rem){
            for(j=i+1;j<=len;j++) a[j-1]=a[j];
            len--;
            return 1;
        }
    }
    return 0;
}
int main()
{
    int i,j,k,rem;
    int no=0,sum=0;
    gets(str); len=strlen(str);
    
    for(i=0;i<len;i++){
        a[i+1]=str[i]-'0';
        sum+=a[i+1];
    }
    qsort(a+1,len,sizeof(int),cmp);
    if(a[1]!=0){
        for(i=2;i<=len;i++) {
            if(a[i]==5){
                for(j=i-1;j>=1;j--) a[j+1]=a[j];
                a[1]=5;
                break;
            }
        }
        if(i>len)  no=1;
    }
    rem=sum%3;
    if(rem!=0){
        if(solve(rem)==0){
            if(solve(3-rem)==0) no=1;
            if(solve(3-rem)==0) no=1;
        }
    }
    if(no==1) printf("impossible\n");
    else{
        while(len>1&&a[len]==0) len--;
        for(;len>=1;len--) printf("%d",a[len]);printf("\n");
    }

    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