The simplest thing is to exhaustively enumerate the arrangements of 9 numbers.
Optimization is to eliminate some impossible situations first. For example, the middle number is a multiple of 2, and the last number is a multiple of 3. And so on.
public static void main(String[] args) {
int[] s = new int[9];
for (int i = 300; i < 999; i+=3) {
int flag = 1;
Set<Integer> set = new HashSet<>();
int a = i / 3;
int b = (i / 3) * 2;
s[0]=a%10;s[1]=a%100/10;s[2]=a/100;
s[3]=b%10;s[4]=b%100/10;s[5]=b/100;
s[6]=i%10;s[7]=i%100/10;s[8]=i/100;
for (int i1 : s) {
if (!set.add(i1) || i1 == 0) {
flag = 0;
break;
}
}
if (flag == 1) {
System.out.println(a + "\t" + b + "\t" + i);
}
}
}
The idea is very simple. Since you want the ratio to be 1:2:3 and each number needs to be used once, then directly enlarge 1, 2, and 3 x times, and then determine whether each number appears only once. 1 requires less than 100 times of magnification to reach 3 digits, so just start from 123. code show as below. int testa() {
http://blog.csdn.net/houyp520...
The simplest thing is to exhaustively enumerate the arrangements of 9 numbers.
Optimization is to eliminate some impossible situations first. For example, the middle number is a multiple of 2, and the last number is a multiple of 3. And so on.
Just write recursion. . Or is it called iteration?
public class Sidney {
}
Liu Rujia’s question.
Enumerate the first number, which is the number accounting for 1 in 1:2:3.
The enumeration range is 123~345. Based on this number, calculate the other two numbers, and then determine whether exactly 9 numbers are used.
The idea is very simple. Since you want the ratio to be 1:2:3 and each number needs to be used once, then directly enlarge 1, 2, and 3 x times, and then determine whether each number appears only once. 1 requires less than 100 times of magnification to reach 3 digits, so just start from 123. code show as below.
int testa()
{
}