leetcode:869. 重新排序得到 2 的幂(数学,中等)

题目:

869. 重新排序得到 2 的幂

分析:枚举什么你要注意啦!不要惯性思维,枚举给的数字啊。

代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
 int n;
 cin>>n;
 vector<int> v;
 int maxx=1;
 while(1)
 {
  if(n==0) break;
  maxx=maxx*10;
  v.push_back(n%10);
  n=n/10;
 }
 sort(v.begin(),v.end());
 int t=1;
    cout<<maxx<<endl;
 while(t<maxx)
 {
  t=t*2;
  cout<<t<<' ';
  //if(t<maxx/10) continue;
  vector<int> v2;
  int n=t;
  while(1)
  {
   if(n==0) break;
   v2.push_back(n%10);
   n=n/10;
  }
  sort(v2.begin(),v2.end());
        if(v2.size()!=v.size()) continue;
  int ok=1;
  for(int i=0;i<v2.size();i++)
  {
   if(v2[i]!=v[i]) 
            {
                ok=0;
                break;
            }
  }
  if(ok) return 1;
 }
 return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42721412/article/details/107454434