Hint 樣例說明: 郭嘉能夠拿到的總武力值從小到大為1、3、4、9、10、12、13……所以第7大的總武力值是13。 對于50%的輸入文件,有k≤5000。 對于100%的輸入文件,有k≤2^31-1。
. . . . . 分析 把k轉換成二進制,第i位代表3^(i-1),如果第i位為1就選。
. . . . . 程序:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;string zfc="";long long ksm(long long a,long long b)
{long long ans=1,x=a;while (b!=0){if (b&1) ans*=x;x*=x;b>>=1;}return ans;
}int main()
{freopen("recruitment.in","r",stdin);freopen("recruitment.out","w",stdout);int n;scanf("%d",&n);while (n--){long long k;scanf("%lld",&k);zfc="";while (k){char zf=(char)(k%2)+'0';zfc=zfc+zf;k/=2;}int l=zfc.length();long long a[1000];memset(a,0,sizeof(a));for (int i=1;i<=l;i++)a[i]=ksm(3,i-1);l--;long long ans=0;for (int i=0;i<=l;i++)if (zfc[i]=='1'){ans=(long long)ans+a[i+1];}printf("%lld\n",ans);}fclose(stdin);fclose(stdout);return 0;
}