题意:
给定一个序列,重排,使后面一个数是前面一个数的2倍或者(1/3);
思路:
法一:建图,跑dfs;
法二:一共有两种关系,题说一定有解,所以左边的一定是右边的数的3倍,或者除去3的倍数的因子以外的因子右边是左边的两倍;这样的话对于每个数除以三并记录个数,然后排序;
法二代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 100 + 7;
int n;
struct node {
ll v, t;
}a[maxn];
bool cmp(node a, node b) {
return ((a.t==b.t&&a.v<b.v)||(a.t>b.t));
}
int main() {
cin >> n;
for(int i = 0; i < n; ++i) {
cin >> a[i].v;
a[i].t = 0;
ll v = a[i].v;
while(v % 3 == 0) {
v /= 3;
a[i].t++;
}
}
sort(a, a+n, cmp);
cout << a[0].v;
for(int i = 1; i < n; ++i) {
cout << " " << a[i].v;
}
return 0;
}