http://codeforces.com/problemset/problem/977/D
解题思路:
1.给一个序列,要求给它重新排序,规则要满足以下其中之一条件:
- 前一个数是后一个数的三倍
- 后一个数是前一个数的两倍
2.序列长度只有100,并且保证答案一定存在,直接裸DFS爆搜答案
import java.util.*;
import static java.lang.System.exit;
public class Main {
static int n;
static long[] a = new long[105];
static boolean book[] = new boolean[105];
static Vector<Long> v = new Vector<>();
static void dfs(int tag,int cnt) { //第一个参数是搜索下标,第二个是已搜索数量
v.add(a[tag]); //搜索到的合法数据入队
if(cnt == n) { //终点条件
for(int i = 0;i < n;i++) {
System.out.print(v.get(i) + " ");
}
exit(0);
}
for(int i = 1;i <= n;i++) {
if(book[i] == true) //已搜过的点跳过
continue;
if(a[i] == a[tag] * 2 || a[i] * 3 == a[tag]) { //条件
book[i] = true;
dfs(i,cnt + 1);
}
}
}
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for(int i = 1;i <= n;i++) {
a[i] = sc.nextLong();
}
for(int i = 1;i <= n;i++) {
for(int j = 1;j <= n;j++) //标记数组初始化
book[j] = false;
book[i] = true;
v.clear(); //结果数组清空
dfs(i,1);
}
}
}