• Divide by three, multiply by two


    Polycarp喜欢玩数字。 他取一些整数x,写在黑板上,然后执行两种运算:
    • 将数字x除以3(x必须可以被3整除);
    • 将数字x乘以2。
    你的问题是重新排序这个序列的元素,使得它可以匹配上述规则。即 每个下一个数字将是前一个数字的两倍,或者是前一个数字的三分之一。
     
    保证答案的存在。

    Input

    输入的第一行包含整数n(2≤n≤100) - 序列中元素的数量。 输入的第二行包含n个整数a1,a2,...,an(1≤ai≤3⋅1018

    Output

    输出n个整数 - 重新排列后的序列,可以是原序列。
     
    保证答案的存在。

    Sample Input

    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 
    方案一:
    用DFS枚举可能的情况(可能性逐渐增高),直至构造出题目要求的序列
    #include <iostream>
    #include <cstdio>
    #include <map>
    #define LL long long int
    using namespace std;
    map<LL,int>mp;
    LL a[111];
    LL ans[111];
    int cur,n;
    
    int DFS(LL b)
    {
        if(cur==n-1)
        {
            ans[cur++]=b;
            return 1;
        }
    
        if(mp[b*2])
        {
            ans[cur++]=b;
            if(DFS(b*2)) return 1;
            else cur--;
        }
    
        if( b%3==0 && mp[b/3])
        {
            ans[cur++]=b;
            if(DFS(b/3)) return 1;
            else return 0;
        }
    
        return 0;
    
    }
    
    
    int main()
    {
        int i;
        cin>>n;
        for(i=0;i<n;i++)
        {
            scanf("%lld",&a[i]);
            mp[a[i]]=1;
        }
        for(i=0;i<n;i++)
        {
            cur=0;
            if(DFS(a[i]) )
                break;
        }
        for(i=0;i<n;i++)
            printf("%lld%c",ans[i],i==n-1?'
    ':' ');
        return 0;
    }

     方案二:

    用cmp自定义排序规则

    #include<iostream>  
    #include<string.h>  
    #include<algorithm>  
    #include<cmath>  
    #include<map>  
    #include<string>  
    #include<stdio.h>  
    #include<vector>  
    #include<stack>
    #include<set>
    using namespace std;
    #define INIT ios::sync_with_stdio(false)
    #define LL long long int
    bool cmp(LL a, LL b) {
        int cnta, cntb;
        cnta = cntb = 0;
        while (a % 3 == 0) {
            a /= 3;
            cnta++;
        }
        while (b % 3 == 0) {
            b /= 3;
            cntb++;
        }
        if (cnta == cntb)
            return a < b;
            return cnta > cntb;
    }
    
    int main() {
        int n;
        LL num[1005];
        while (cin >> n) {
            for (int i = 0;i < n;i++) {
                cin >> num[i];
            }
            sort(num, num + n, cmp);
            for (int i = 0;i < n;i++) {
                cout << num[i] << (i == n - 1 ? "
    " : " ");
            }
        }
        return 0;
    }

  • 相关阅读:
    数据结构相关知识
    设计模式
    常用排序算法
    算法之---堆的简单介绍
    树和二叉树简介
    列表查找以及二分查找
    算法基础
    Python之函数(自定义函数,内置函数,装饰器,迭代器,生成器)
    Python学习【第2篇】:Python数据结构
    统计一篇英文文章内每个单词出现频率,并返回出现频率最高的前10个单词及其出现次数
  • 原文地址:https://www.cnblogs.com/coder-tcm/p/9220331.html
Copyright © 2020-2023  润新知