(HDU)1390 -- Binary Numbers(二进制数)

题目链接:http://vjudge.net/problem/HDU-1390

把一个数变成二进制数,找出为1的是哪几位,最低位为0。

变成二进制数的过程中模拟查找就可以了。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <iostream>
 5 #include <algorithm>
 6 #include <string>
 7 #include <cstdlib>
 8 
 9 using namespace std;
10 
11 int main()
12 {
13     int t,n,cnt,ans[1000],i,place;
14     scanf("%d",&t);
15     while(t--)
16     {
17         place=0,cnt=0;
18         memset(ans,0,sizeof(ans));
19         scanf("%d",&n);
20         while(n)
21         {
22             if(n%2==1)
23             {
24                 cnt++;
25                 ans[cnt]=place;
26             }
27             n/=2;
28             place++;
29         }
30         if(cnt==1) printf("%d
",ans[cnt]);
31         if (cnt>1)
32         {
33             for(i=1;i<cnt;i++)
34                 printf("%d ",ans[i]);
35             printf("%d
",ans[i]);
36         }
37     }
38     return 0;
39 }
原文地址:https://www.cnblogs.com/ACDoge/p/6137809.html