题目:有 1、2、3、4 四个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
package myself;
/**
* @Auther QY
* @Date 2023/12/12
*/
public class Eleven {
public static void main(String[] args) {
play();
}
public static void play() {
int count = 0;
for (int i = 1; i < 5; i++) {
for (int j = 1; j < 5; j++) {
for (int k = 1; k < 5; k++) {
if (i != j && j != k && i != k) {
count++;
System.out.println(i * 100 + j * 10 + k);
}
}
}
}
System.out.println("共计" + count + "种");
}
}
上面是面向题目编程,可以再看看递归的方法
static int[] res = new int[4];
static boolean[] st = new boolean[5];
public static void play2(int n) {
if (n > 3) {
System.out.println(res[1] * 100 + res[2] * 10 + res[3]);
n--;
return;
}
for (int i = 1; i <= 4; i++) {
if (!st[i]) {
st[i] = true;
res[n] = i;
play2(n+1);
st[i] = false;
}
}
}