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

大牛门 帮忙看看一 下

Posted by yanhaofeng at 2005-07-29 17:44:58 on Problem 1702
#include<stdio.h>
int main()
{ int d[20],T,W,i,j,ca,D[20],L[20],R[20],r,l,flag,c;
   d[0]=1;d[1]=3;D[0]=1;D[1]=4;
   for(i=2;i<=19;i++){d[i]=d[i-1]*3;D[i]=D[i-1]+d[i];}
   //1 3 9    (9+3)-1=11 (9+1)-3=7  9-(3+1)=5  
  scanf("%d",&T);
   for(ca=1;ca<=T;ca++)
    { scanf("%d",&W);
      //W=D[19];
     l=0;r=0;
     flag=2;
     while(W>0)
     { if(W==1) 
       {if(flag==2){ r++;R[r]=1;break;}
        else {l++;L[l]=1;break;}
       }    
     for(i=19;i>=1;i--)
      //if(W<1)break;
      { if(W<=D[i]&&W>D[i-1])
       { if(flag==2)
         { r++;R[r]=d[i]; W-=d[i];
           if(W<0)
           { flag=1;W=-W;break;}
         }
         else 
         { l++;L[l]=d[i];W-=d[i];
           if(W<0){ flag=2;W=-W;break;}
           break;
         }
         if(W==0)break;
       }
       
     }
     }
     //if(W==1)
     //{ if(flag==1){ l++;L[l]=1;W=0; }
      // else { r++;R[r]=1;W=0;}
     //}
     if(l==0)printf("empty");
     for(i=l;i>=1;i--) { printf("%d",L[i]);if(i>1)printf(",");}
     printf(" ");
     for(i=r;i>=1;i--){printf("%d",R[i]);if(i>1)printf(",");}
     printf("\n");
    }        
  
}
/*

3
9
5
20


Sample Output 

empty 9
1,3 9
1,9 3,27

*/


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