重排列得到2的幂(51Nod-2515)

题目

小b有一个数n,现在她想把n的每一位重排列,使得得到的结果为2的幂次。

请问小b能得到2的幂次吗?

注意重排列后不允许有前导0。

样例解释:46重排列成64,为2^6。

输入

输入一个数N,其中1≤N≤10^9

输出

满足条件,输出“true”;
不满足,则输出“false”。

输入样例

46

输出样例

true

思路:提前将 2^i 打好表,然后对于数字 n 去分解每一位,再对所有位进行全排列,于表中的数字进行比较即可,要注意的是,由于 n 最大到 1e9,打表需要使用 map

源程序

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 4000000+5;
const int dx[] = {0,0,-1,1,-1,-1,1,1};
const int dy[] = {-1,1,0,0,-1,1,-1,1};
using namespace std;

int bit[15],tot;
map<int,int> mp;
void init(){
    for(int i=0; i<31; ++i)
        mp[1<<i]=1;
}
int main() {
    init();

    int n;
    scanf("%d",&n);
    while(n) {//分解每一位
        bit[tot++]=n%10;
        n/=10;
    }
    sort(bit,bit+tot);
    bool flag=false;
    do {//对所有位数全排列
        int sum=0;
        if(bit[0]==0)
            continue;
        for(int i=0; i<tot; ++i)
            sum=sum*10+bit[i];
        if(mp[sum]==1) {//逐个判断每一位
            flag=true;
            break;
        }
    } while(next_permutation(bit,bit+tot));

    if(flag)
        puts("true");
    else
        puts("false");
    return 0;
}

 

posted @   老程序员111  阅读(14)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 提示词工程——AI应用必不可少的技术
· 地球OL攻略 —— 某应届生求职总结
· 字符编码:从基础到乱码解决
· SpringCloud带你走进微服务的世界
点击右上角即可分享
微信分享提示