HDU-2502-月之数

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=2502

分析:

比如n=4时,有:

1000 1001 1010 1011 1100 1101 1110 1111 可以看到。除了第一位剩下的有 000 001 010 011 100 101 110 111 可以数一下,0和1的总个数一半对一半。于是算一下0和1总个数,除以2就好了。

即为:2^(n-2)*(n-1)  

再加上第一位1总个数:2^(n-1) 得月之数 

即answer = 2^(n-2)*(n-1) + 2^(n-1)

代码

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>

int main(void)
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
printf("%.lf ",pow(2,n-2)*(n-1)+pow(2,n-1));
}
return 0;
}

原文地址:https://www.cnblogs.com/liudehao/p/4132937.html