月之数:
#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就好了。