abcde / fghij = n

问题:

输入正整数n,按从小到大的顺序输出所有形如abcde/fghij=n的表达式,

其中a~j恰好为数字0~9的一个排列,2<=n<=79。


样例输入: 62

样例输出: 79546/01238=62

                   94736/01528=62


解:

public class Main {

	static int[] a = new int[10];

	public static boolean fun(int i) {
		String s = String.valueOf(i);
		for (int j = 0; j < s.length(); j++) {
			int b = Integer.valueOf(String.valueOf(s.charAt(j)));
			if (a[b] == 0)
				a[b] = 1;
			else
				return false;
		}
		return true;
	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		da: for (int i = 10234; i <= 98765; i++) {
			Arrays.fill(a, 0);
			if (!fun(i))
				continue;
			double m = (double) i / n;
			int y = (int) m;
			if ((Math.floor(m) != Math.ceil(m)) || !fun(y)) {
				continue;
			}
			for (int k = 1; k < 10; k++) {
				if (a[k] == 0)
					continue da;
			}
			if (a[0] == 0)
				System.out.println(i + "/0" + y + "=" + n);
			else
				System.out.println(i + "/" + y + "=" + n);
		}
	}
}

你可能感兴趣的:(Java)