zoj 1003
http://blog.csdn.net/cxiaokai/article/details/6852706
抄袭上述算法:
通过每个数只使用一次,来检测是否存在相容解。堆栈最深为100,步骤数 100!
输入结束的判断还蛋疼的不能用hasnext() ..
import java.util.Scanner; public class Main { static boolean flag = false; static boolean check(int b, int s, int n) { for (int i = n; i > 1; i--) { if (s % i == 0 && check(b, s / i, i - 1)) return true; if (b % i == 0 && check(b / i, s, i - 1)) return true; } if (s == 1) { flag = true; } if (!flag) { return true; } if (s == 1 && b == 1) { return true; } else { return false; } } public static void main(String[] args) { int a, b; Scanner sc = new Scanner(System.in); while (sc.hasNextInt()) { a = sc.nextInt(); b = sc.nextInt(); flag = false; int small = a > b ? b : a; int big = a > b ? a : b; if (!check(big, small, 100)) { System.out.println(small); } else { System.out.println(big); } } } }
------------------------------------------------------------------------------------------------------------------------------------------
以下是我没想完的想法
我想通过获取两个数的乘积形式,枚举所有相同因子组合检测是否存在相容。但是在如何枚举上不知道如何去做。
胎死腹中、、
import java.util.Scanner; public class Main { /** * 获得乘积形式,如果有因子大于100,直接返回false * * @param chain * @param num * @return */ static boolean getChain(int[] chain, int num) { int[] primes = new int[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 }; int pp = 0; while (num != 1) { if (num % primes[pp] == 0) { num /= primes[pp]; chain[primes[pp]]++; pp = 0; } else { if (pp == primes.length - 1) { return false; } pp++; } } return true; } public static void main(String[] args) { int[] hChain = new int[101]; int[] lChain = new int[101]; int[] comChain = new int[101]; int higher, lower, tmp; boolean hf = false, lf = false; Scanner sc = new Scanner(System.in); higher = sc.nextInt(); lower = sc.nextInt(); if (higher < lower) { tmp = higher; higher = lower; lower = tmp; } hf = getChain(hChain, higher); lf = getChain(lChain, lower); if (lf && hf) { //枚举组合 } else if (!hf && lf) { System.out.println(lf); } else if (!lf) { System.out.println(hf); } } }