题解:
袁绍的刁难
(File IO): input:recruitment.in output:recruitment.outDescription
这时候有很多很多的将领到袁绍处报到(别人家底厚,四世三公哪~~),每个将领的编号依次为1、2、3……N,第i个将领的武力值为3^(i-1)。
袁绍需要我们的郭嘉大大招纳任意个将领,而郭嘉选中的将领有一个“总武力值”为各个将领的武力值之和。例如:郭嘉这一次招募了第一个将领和第三个将领,那么“总武力值”为1+9=10。
袁绍想知道,他可以获得的第k大的“总武力值”是多少,请你帮助我们的郭嘉大大告诉袁绍这个第k大的“总武力值”。
从文件中读入k,输出郭嘉能够获得的,第k大的“总武力值”。
Input
Output
Sample1 Input
1
7
Sample1 Output
13
Sample2 Input
10
69
18
42
24
46
54
5
78
27
34
Sample2 Output
739
84
273
108
282
336
10
768
112
246
Data Constraint
对于100%的输入文件,有k≤2^31-1。
Hint
样例说明:
1 int a[1000];
2 int o=1,k;
3 scanf("%d",&k);
4 while(k!=0)
5 {
6 a[o]=k%2;
7 k/=2;
8 o++;
9 }
10 for(int i=o-1;i>=1;i--)
11 printf("%d",a[i]);
这个需要倒叙输出,看到这,就联想到了前面的需要翻转,那我们为什么不直接不翻转来直接对应呢?想到这就可以更简化了,下面上代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n;
long long k,h;
long long t[35];
void dabiao();
int main()
{
freopen("recruitment.in","r",stdin);//如果题目没要求可以注释掉
freopen("recruitment.out","w",stdout);
dabiao();
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int o=1;//记录现在对应的是第几位数
h=0;
scanf("%lld",&k);
while(k!=0)
{
long long p;
p=k%2;
if(p==1) h+=t[o];
k/=2;
o++;
}
printf("%lld\n",h);
}
return 0;
}
void dabiao()//打表
{
t[1]=1;
t[2]=3;
t[3]=9;
t[4]=27;
t[5]=81;
t[6]=243;
t[7]=729;
t[8]=2187;
t[9]=6561;
t[10]=19683;
t[11]=59049;
t[12]=177147;
t[13]=531441;
t[14]=1594323;
t[15]=4782969;
t[16]=14348907;
t[17]=43046721;
t[18]=129140163;
t[19]=387420489;
t[20]=1162261467;
t[21]=3486784401;
t[22]=10460353203;
t[23]=31381059609;
t[24]=94143178827;
t[25]=282429536481;
t[26]=847288609443;
t[27]=2541865828329;
t[28]=7625597484987;
t[29]=22876792454961;
t[30]=68630377364883;
t[31]=205891132094649;
}
by:ルオ・テンイの锦依卫
未经作者允许,禁止转载!