poj 1702(三进制计算)

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int main(){
    int i,t,w,a[25],data[25],ans[25],dd,len,count;
    bool flag;
    scanf("%d",&t);
    for(i=0,dd=1;i<20;i++,dd*=3){
        data[i] = dd;
    }
    while(t--){
        scanf("%d",&w);
        memset(a,0,sizeof(a));
        i = 0;
        while(w>0){
            a[i++] = w%3;
            w /= 3;
        }
        len = i;
        for(i=0;i<len;i++){
            if(a[i]==2) a[i] = -1,a[i+1]++;
            if(a[i]==3) a[i] = 0,a[i+1]++;
        }
        if(a[len])len++;
        flag = true;
        count = 0;
        for(i=0;i<len;i++){
            if(a[i]==-1){
                flag = false;
                ans[count++] = data[i];
            }
        }
        if(flag)printf("empty ");
        else{
            for(i=0;i<count-1;i++){
                printf("%d,",ans[i]);
            }
            printf("%d ",ans[count-1]);
        }
        flag = true;
        count = 0;
        for(i=0;i<len;i++){
            if(a[i]==1){
                flag = false;
                ans[count++] = data[i];
            }
        }
        if(flag)printf("empty\n");
        else{
            for(i=0;i<count-1;i++){
                printf("%d,",ans[i]);
            }
            printf("%d\n",ans[count-1]);
        }
    }
    return 0;
}

 

上一篇:摸鱼浮夸


下一篇:光模块及速率