20位最多能表示的数字1048575,遍历长度为n的值,然后加上其二进制位即可
#include<stdio.h>
int length(int n) {
int count = 0;
while (n) {
count++;
n>>=1;
}
return count;
}
int onenum(int n) {
int count = 0;
while (n) {
if (n&1==1) {
count++;
}
n>>=1;
}
return count;
}
int main(void)
{
int T;
scanf("%d", &T);
int sum, n;
while (T--) {
sum = 0;
scanf("%d", &n);
int k = 1;
for (int i = 1; i <= 1048575; i++) {
if (length(i) == n) {
sum += onenum(i);
}
}
printf("%d\n", sum);
}
return 0;
}