poj 1702 思路很简单 写的代码真心挫

#include <iostream>
using namespace std;

int a[20];
int l[20];
int r[20];
int b[20];
bool flag;
int lcnt,rcnt;

void calc(int n)
{
     int i = 0;
     while(n/3)
     {
           a[i++] = n%3;
           n = n/3;
     }
     a[i] = n;
}

void balance()
{
     int i;
     for(i=0;i<20;i++)
     {
		 if(a[i]==3){a[i+1]++;a[i]=0;}
             if(a[i]==2)
             {
                    l[i] =1;
                    a[i+1]++;
					a[i]=0;
					flag = true;
             }
     }
     for(i=0;i<20;i++)
     r[i] = a[i];
	 for(i=0;i<20;i++)
	 {
		 lcnt += l[i];
		 rcnt += r[i];
	 }
}

int main()
{
    
    int i,j;

    b[0] =1;
    for(i = 1;i<20;i++)b[i] = 3*b[i-1];
  
    int cas,n;
    cin>>cas;
    while(cas--)
    {
         cin>>n;
		 flag = false;
		 lcnt = 0;
		 rcnt = 0;
         memset(a,0,sizeof(a));
         memset(l,0,sizeof(l));
         memset(r,0,sizeof(r));
         calc(n);
         balance();
		 if(!flag)cout<<"empty ";
		 else
		 {
			 for(i=0;i<20 && lcnt>1;i++)
			 {
				 if(l[i]==1){
					 cout<<b[i]<<",";
					 lcnt--;}
			 }
			 for(j=i;j<20;j++)
				 if(l[j]==1)cout<<b[j]<<" ";
		 }
             for(i=0;i<20 && rcnt>1;i++)
			 {
				 if(r[i]==1){
					 cout<<b[i]<<",";
					 rcnt--;}
			 }
			 for(j=i;j<20;j++)
				 if(r[j]==1)cout<<b[j]<<endl;
    }
    system("pause");
}
posted @ 2011-06-23 15:15  dapanshe  阅读(217)  评论(0编辑  收藏  举报