Problem B: Ternarian Weights

大致题意:使用三进制砝码采取相应的措施衡量出给定的数字
主要思路:三进制,如果 大于 2 向前进位,之前一直没写好放弃了,这次终于写好了……

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <cctype>

const double Pi = atan(1) * 4;

using namespace std;
long long base[] = {1,1,3,9,27,81,243,729,2187,6561,19683,59049,177147,531441,1594323,4782969,14348907,43046721,129140163,387420489,1162261467,3486784401};
int a[100];
int tt[110];
int main()
{
    //freopen("input.in","r",stdin);
    //freopen("output.in","w",stdout);
    int n;
    cin >> n;
    while(n--){
        int x;
        cin >> x;
        memset(a,0,sizeof(a));
        memset(tt,0,sizeof(tt));
        int tmp = x;
        while(tmp){
            a[++a[0] ] = tmp % 3;
            tmp /= 3;
        }
        for(int i = 1;i <= a[0];i++){
            if(a[i] == 2){
                a[i+1]++;
                a[0] = max(i+1,a[0]);
                tt[ ++tt[0] ] = i;
                a[i] = 0;
            }
            if(a[i] == 3){
                a[i+1]++;
                a[0] = max(i+1,a[0]);
                a[i] = 0;
            }
        }
        cout << "left pan:";
        for(int i = tt[0];i > 0;i--){
            cout << " " << base[ tt[i] ];
        }
        cout << endl;
        cout << "right pan:";
        for(int i = a[0];i > 0;i--){
            if(!a[i])
                continue;
            cout << " " << base[i];
        }
        cout  << endl;
        if(n)
            cout << endl;
    }
    return 0;
}
View Code

 

posted @ 2015-01-28 17:25  寒饼干  阅读(112)  评论(0编辑  收藏  举报