ZZULIOJ 1149: 组合三位数之二,Java
题目描述
把1,2,3,4,5,6,7,8,9,组成三个三位数(每个数只能用一次),第二个数是第一个数的2倍,第三个数是第一个数的3倍,这三个三位数各是多少?答案可能有很多组,请按第一个数的升序顺序输出每组的三个三位数。
输入
无
输出
输出所有满足条件的三位数组合,按第一个数的升序顺序输出。
样例输出 Copy
192 384 576
.........
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;public class Main {public static boolean check(int a, int b, int c) {int[] st = new int[10];for (int ai : new int[]{a, b, c}) {while (ai > 0) {int x = ai % 10;ai /= 10;st[x]++;}}for (int i = 1; i <= 9; i++) {if (st[i] != 1) {return false;}}return true;}public static void main(String[] args) throws IOException {BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));for (int i = 123; i <= 329; i++) {if (check(i, i * 2, i * 3)) {bw.write(i + " " + i * 2 + " " + i * 3 + "\n");}}bw.close();}
}