您的位置:首页 > 其它

2016华中农业大学预赛 E 想法题

2016-05-15 16:52 274 查看

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)

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#define ll __int64
using namespace std;
int a[100005];
int n;
int ans;
int exm;
int main()
{
int sum=1;
int jishu=1;
int exm=1;
for(int i=1;i<=100;i++)
{
a[jishu]=sum;
exm*=3;
sum=sum+exm;
jishu++;
}
while(scanf("%d",&n)!=EOF)
{
for(int i=1;i<=n;i++)
{
scanf("%d",&exm);
for(int j=1;j<=jishu;j++)
{
if(exm<=a[j])
{
ans=j;
break;
}
}
cout<<ans<<endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: