一个数是否为2的次幂

 1 #include<iostream>
 2 #include<vector>
 3 using namespace std;
 4 int fun(int i)
 5 {
 6     if(i==1){
 7     return 0;
 8     }
 9       if (i&&!(i&(i-1)))

10             return 1;
11 
12       else
13             return 0;
14 }
15 
16 int main()
17 {
18       vector<int> vel;
19       int x;
20       bool A;
21       while(cin>>x)
22       {
23             A=fun(x);
24             vel.push_back(A);
25       }
26       vector<int>::iterator it;
27       for(it=vel.begin();it!=vel.end();it++)
28       {
29             if(*it==1)
30             {
31                 cout<<"true"<<endl;
32             }
33             else{
34             cout<<"false"<<endl;
35             }
36       }
37       return 0;
38 }


输入若干行,判断是否为2的次幂。。。。。

posted on 2016-09-27 12:17  邗影  阅读(190)  评论(0编辑  收藏  举报

导航