hdu2502 月之数

来源:互联网 发布:看不见的战争 知乎 编辑:程序博客网 时间:2024/04/30 01:38

1008.月之数

题意为,求出二进制数位数为n数的个数。由于1<=n<=20,由于数据范围很小,暴力预处理出来,然后输出答案即可。


#include<cstdio>#include<cstring>const int maxn=100;int f[maxn];int main(){    memset(f,0,sizeof(f));    int ans=0,now=0;    while (ans<=20)    {        ans=0;        int st=now,w=0;        while (st>0)        {            ans++;            if (st&1) w++;            st>>=1;        }        if (ans>20) break;        f[ans]+=w;        now++;    }    int t;    scanf("%d",&t);    while (t--)    {        int n;        scanf("%d",&n);        printf("%d\n",f[n]);    }    return 0;}


0 0
原创粉丝点击