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啊 什么情况啊 。。。 求大牛解答。。(PS:新手一个AND代码有点长)

Posted by Burglar at 2014-01-03 11:21:41 on Problem 2718
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define NF -0x3f3f3f3f
int ans;
int x[11];
int a1[11];
int ak[11];
int b1[11];
int k,a,b,e,h,z,mine;
int po(int n){
    if(n==0)
    return 1;
    int y,p;
    p=1;
    for(y=0;y<n;y++){
        p=10*p;
    }
    return p;
}
int max(int n){
    int i,r,j;
    r=NF;
    for(i=0;i<n;i++){
        if(x[i]>r&&ak[i]!=-1)
        r=x[i];
    }
    for(j=0;j<n;j++){
        if(x[j]==r){
            ak[j]=-1;
            break;
        }
    }
    return r;
}
int min(int n){
    int i,r,j;
    r=INF;
    for(i=0;i<n;i++){
        if(x[i]<r&&ak[i]!=-1)
        r=x[i];
    }
    for(j=0;j<n;j++){
        if(x[j]==r){
            ak[j]=-1;
            break;
        }
    }
    return r;
}
int solution1(int h){
  int w,c,v,s,ans1;
   v=h/2;
   for(w=0;w<h;w++){
       memset(ak, 0, sizeof(ak));
       memset(a1, 0, sizeof(a1));
       memset(b1, 0, sizeof(b1));
       c=v-1;
       if(z==0){
           if(x[w+1]==0||x[w+2]==0)
           continue;
           a1[0]=x[w+1];
           ak[w+1]=-1;
           b1[0]=x[w+2];
           ak[w+2]=-1;
           a=a1[0]*po(c);
           b=b1[0]*po(c);
           for(s=1;s<v;s++){
               a1[s]=max(h);
               c--;
               a+=a1[s]*po(c);
           }
           c=v-1;
           for(s=1;s<v;s++){
               b1[s]=min(h);
               c--;
               b+=b1[s]*po(c);
           }
       }
       else if(z==1){
           if(x[w]==0||x[w+1]==0)
           continue;
           a1[0]=x[w];
           ak[w]=-1;
           b1[0]=x[w+1];
           ak[w+1]=-1;
           a=a1[0]*po(c);
           b=b1[0]*po(c);
           for(s=1;s<v;s++){
               a1[s]=max(h);
               c--;
               a+=a1[s]*po(c);
           }
           c=v-1;
           for(s=1;s<v;s++){
               b1[s]=min(h);
               c--;
               b+=b1[s]*po(c);
           }
       }       if((a-b)>0)
       ans1=a-b;
       else
       ans1=b-a;
       if(ans1<mine)
       mine=ans1;
   }
   return mine;
}
int solution2(int h){
   int c,v,s,ans1;
   v=(h/2)+1;
       memset(ak, 0, sizeof(ak));
       memset(a1, 0, sizeof(a1));
       memset(b1, 0, sizeof(b1));
       c=v-1;
       if(z==0){
           a1[0]=x[1];
           ak[1]=-1;
           b1[0]=x[h-1];
           ak[h-1]=-1;
           a=a1[0]*po(c);
           b=b1[0]*po(c-1);
           for(s=1;s<v;s++){
               a1[s]=min(h);
               c--;
               a+=a1[s]*po(c);
           }
           c=v-1;
           for(s=1;s<(h-v);s++){
               b1[s]=max(h);
               c--;
               b+=b1[s]*po(c-1);
           }
       }
       else if(z==1){
         a1[0]=x[0];
           ak[0]=-1;
           b1[0]=x[h-1];
           ak[h-1]=-1;
           a=a1[0]*po(c);
           b=b1[0]*po(c-1);
           for(s=1;s<v;s++){
               a1[s]=min(h);
               c--;
               a+=a1[s]*po(c);
           }
           c=v-1;
           for(s=1;s<(h-v);s++){
               b1[s]=max(h);
               c--;
               b+=b1[s]*po(c-1);
           }
       }
       mine=a-b;
   return mine;
}
int main(){
    scanf("%d\n",&k);
    int q;
    for(e=0;e<k;e++){
         memset(x, 0, sizeof(x));
        mine=INF;
        a=0;
        b=0;
        h=0;
        ans=0;
        char ch;
          while((ch=getchar())!='\n')
        {
            if(ch==' ')
                continue;
            x[h++]=ch-'0';
        }
        if(h==1){
            printf("%d\n",x[0]);
            continue;
        }
        sort(x,x+h);
        if(x[0]==0){
            z=0;
        }
        else if(x[0]!=0){
            z=1;
        }
        if(h%2==0){
            ans=solution1(h);
        }
        else if(h%2==1){
            ans=solution2(h);
        }
        printf("%d\n",ans);
    }
    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