月之數:
#include<iostream>
#include<cmath>
using namespace std;
int main(){
int n;
cin>>n;
while(n--){
int m;
cin>>m;
int count=0;
count+=pow(2.0,m-1)+(m-1)*(pow(2.0,m-1))/2;
cout<<count<<endl;
}
return 0;
}當n=4時,有:
1000
1001
1010
1011
1100
1101
1110
1111
可以看到。除了第一位剩下的有
000
001
010
011
100
101
110
111
可以數一下,0和1的總個數一半對一半。於是算一下0和1總個數,除以2就好了。
本文出自 “菜鳥的進階之路” 博客,請務必保留此出處http://beyond316.blog.51cto.com/7367775/1275620