5 solutions
-
1
先将2的幂次方都计算出来,存入一个数组中。 再用二分查找看给的数是否在数组中。 题解这里格式有点乱,。。。。
#include<bits/stdc++.h> using namespace std; typedef long long LL; int main(){ int m; cin>>m; vector<LL> a; int i; for(i=0;i<=100;i++){ a.push_back(pow(2,i)); } LL n; while(m--){ cin>>n; /////////////二分查找模板//////////////////// int left = 0; int right = a.size() - 1; bool flag = false; while(left<=right){ int middle = left + ((right - left)/2); if(a[middle]>n){ right = middle - 1; } else if(a[middle]<n){ left = middle + 1; } else{ flag = true; break; } } /////////////二分查找模板//////////////////// if(flag == true){ cout<<"YES"; } else{ cout<<"NO"; } cout<<endl; } return 0; }
Information
- ID
- 47
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 8
- Tags
- # Submissions
- 1493
- Accepted
- 232
- Uploaded By