Problem E: Balance

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 205  Solved: 64
[Submit][Status][Web Board]

Description

    Every night Diao Ze is dreaming about the gold medal in the 1540th">  ACM-ICPC World Final. Not only does it represent the honor, but also Diao Ze likes the gold, and we know it is blingbling. Whenever Diao Ze gets a gold medal in his dream, he would weigh it again and again. Gosh, so tightfisted he is. For fear of losing any gold, he wants to know the certain weight of this gold medal. So he asks you for help in his dream.

 

    Now you have a counter balance, like the picture above (you can put weight counters on both of the pans of the scale). Diao Ze wants to get the weight of the medal accurately, and he knows that the medal could not be heavier than N grams. If it is possible for Diao Ze to make the scale balanced with some certain different counter (any weight of the counter which has integer weight of grams is available for you, and the amount is infinite). Diao Ze could get the exact weight of the medal from the nominal weight of the counter. Pay attention, the counter with the same weight cannot be used more than once!

Input

    The first line contains an integer T, indicating the total number of test cases. In each test case there is only one line contains an integer , indicating the maximum possible weight of the gold medal.

 

Output

    For each test case, output the answer indicating the minimum number of counters, in that condition, you can make the scale balanced if the medal is x grams, no matter what x is, but satisfies .

 

Sample Input

3
3
4
5

Sample Output

2
2
3

HINT

 

    In the second sample, N=4, we can use 2 counter with nominal weight 1 grams and 3 grams. If the medal is 1 gram, just use the counter with 1 gram. If the medal is 2 grams, use the counter with 3 grams and 1 gram on two pans, the medal is on the same pan with counter 1. That means 2=3-1. If the medal is 3 grams, just use the counter with 3 grams. And if the medal is 4 grams, use the counter with 3 grams and 1 gram on the same pan, the medal is on the other pan. That means 4=3+1. So all the possible weight between 1 and 4 can be got.

 

题意:思维题  t组数据  用最少的砝码称出 1~exm 质量的物品  输出 砝码的最少数量

 

题解:把题目分析出来 可以抽象为 打一个3的次幂的前n项和的表 判断exm的位置 

         

         exm        ans          砝码            

           1          1               1                 

           2          2              1,3              

           3          2              1,3              

           4          2              1,3             

           5          3              1,3,9                 exm=5与exm=4相比增加了质量为5的物品 1+3最多能够表示4   可以这样表示5=9-(1+3)

           6          3              1,3,9  

  ....

 

           14        4              1,3,9,27            14=27-(1+3+9)

 

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 #include<map>
 6 #define ll __int64
 7 using namespace std;
 8 int a[100005];
 9 int n;
10 int ans;
11 int exm;
12 int main()
13 {
14     int sum=1;
15     int jishu=1;
16     int exm=1;
17     for(int i=1;i<=100;i++)
18     {
19         a[jishu]=sum;
20         exm*=3;
21         sum=sum+exm;
22         jishu++;
23     }
24     while(scanf("%d",&n)!=EOF)
25     {
26         for(int i=1;i<=n;i++)
27         {
28             scanf("%d",&exm);
29             for(int j=1;j<=jishu;j++)
30             {
31                 if(exm<=a[j])
32                 {
33                     ans=j;
34                     break;
35                 }
36             }
37             cout<<ans<<endl;
38         }
39     }
40     return 0;
41 }