zoukankan      html  css  js  c++  java
  • 除以三,乘以二

    泰泰学长喜欢玩数字(不知道什么奇怪的癖好)。他在黑板上写下一个数字 x ,然后进行 n-1 次以下两种操作:

    • x 除以3 (必须能整除才能进行,即 x mod 3=0)
    • x 乘以2

    每次操作完成后,泰泰学长在黑板上写上这个操作后的新数字,并让这个新数字作为新的 x 继续下一次操作。最后黑板上有 n 个数字。

    由于泰泰学长是随机在黑板上的位置写数字的,所以他最后忘记了顺序。现在泰泰学长只知道所有的数字,你能帮助泰泰学长找出一种可能的序列吗?

    保证答案一定存在。

    Input

    第一行是数字总数 n (2 ≤ n ≤ 100)。第二行包含 n 个数字a1,a2,…,an (1 ≤ ai ≤3×10^18),注意是不按顺序的。

    Output

    输出 n 个数字,按照泰泰学长写数字的顺序排列。

    保证答案一定存在。

    Examples

    Input
    6
    4 8 6 3 12 9
    Output
    9 3 6 12 4 8 
    Input
    4
    42 28 84 126
    Output
    126 42 84 28 
    Input
    2
    1000000000000000000 3000000000000000000
    Output
    3000000000000000000 1000000000000000000 

    Note

    第一个样例中的可能顺序为:[9, 3, 6, 12, 4, 8]。此时开始的 x=9

    AC代码1:

    #include<cstdio>
    #include <map> 
    #include<iostream>
    #include<string>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    inline int read() {int x=0,f=1;char c=getchar();while(c!='-'&&(c<'0'||c>'9'))c=getchar();if(c=='-')f=-1,c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return f*x;}
    typedef long long ll;
    const int maxn=1000;
    ll a[maxn];
    ll b[maxn];
    map<ll,int>mp;
    int main()
    {
        ll n;
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>a[i];
            mp[a[i]]=1;
        }
        ll ans;
        int j;
        for(int i=0;i<n;i++){
            b[0]=a[i];
            ans=a[i];
            for(j=1;j<n;j++){
                if(mp[ans/3]==1&&ans%3==0){
                    b[j]=ans/3;
                    mp[ans/3]=0;
                    ans/=3;
                }
                else if(mp[ans*2]==1){
                    b[j]=ans*2;
                    mp[ans*2]=0;
                    ans*=2;
                }
                else{
                    break;
                }
            }
            if(j==n){
                for(int k=0;k<n;k++){
                    printf("%lld ",b[k]);
                }
            }
            else{
                for(int k=0;k<n;k++){
                    mp[a[k]]=1;
                } 
            }        
        }
    }

    AC代码2(dfs):

    #include<cstdio>
    #include <map>
    #include<iostream>
    #include<string>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    inline int read() {int x=0,f=1;char c=getchar();while(c!='-'&&(c<'0'||c>'9'))c=getchar();if(c=='-')f=-1,c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return f*x;}
    typedef long long ll;
    const int maxn=1000;
    map<ll,int>mp;
    ll a[maxn];
    ll b[maxn];
    int flag=0;
    int n;
    void dfs(ll x,int p){
        if(flag){
            return ;
        }
        if(p==n){
            flag=1;
            return ;
        }
        if(x%3==0&&mp[x/3]==1){
            b[p]=x/3;
            mp[x/3]=0;
            dfs(x/3,p+1);
            mp[x/3]=1;
        }
        else if(mp[x*2]==1){
            b[p]=x*2;
            mp[x*2]=0;
            dfs(x*2,p+1);
            mp[x*2]=1;
        }
    }
    int main(){
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>a[i];
            mp[a[i]]=1;
        }
        for(int i=0;i<n;i++){
            b[0]=a[i];
            dfs(a[i],1);
            if(flag){
                for(int j=0;j<n;j++){
                    printf("%lld ",b[j]);
                }
                break;
            }
        }
        return 0;
    }
  • 相关阅读:
    PythonのTkinter基本原理
    使用 Word (VBA) 分割长图到多页
    如何使用 Shebang Line (Python 虚拟环境)
    将常用的 VBScript 脚本放到任务栏 (Pin VBScript to Taskbar)
    关于 VBScript 中的 CreateObject
    Windows Scripting Host (WSH) 是什么?
    Component Object Model (COM) 是什么?
    IOS 打开中文 html 文件,显示乱码的问题
    科技发展时间线(Technology Timeline)
    列置换密码
  • 原文地址:https://www.cnblogs.com/lipu123/p/12238276.html
Copyright © 2011-2022 走看看