【java】【简单数学问题】如何实现1至6,实现一个不重复的4位数

 
  
方法一(简单粗暴的)
 方法2
int count = 0;
int n = 6;
for (int a = 1; a <= n; a++) {
for (int b = 1; b <= n; b++) {
for (int c = 1; c <= n; c++) {
for (int d = 1; d <= n; d++) {
if (a != b && a != c && a != d && b != c && b != d
 && c != d) {
System.out.println(a+" "+b+" "+c+" "+d);
count++;
 }
 }
 }
 }
 }
方法二
int count = 0;
for (int i = 1234; i <= 6543; i++) { // 四位数每位数1~6
int qw = i / 1000;
int bw = i % 1000 / 100; // bw=i/100%10
int sw = i % 100 / 10; // sw=i/10%10
int gw = i % 10;
if (qw != bw && qw != sw && qw != gw && bw != sw && bw != gw
&& sw != gw) {
count++;//注意count在此是错误的,会使该数据的1237-1240计数,导致1236为count==3;1243的count==8;1241,1242则会因为之前的if位数重复判断跳出不计数
if (gw == 7 || gw == 8 || gw == 9 || gw == 0) {
continue;
}
if (sw == 7 || sw == 8 || sw == 9 || sw == 0) {
continue;
}
if (bw == 7 || bw == 8 || bw == 9 || bw == 0) {
continue;
}
if (qw == 7 || qw == 8 || qw == 9 || qw == 0) {
continue;
}
count++;
System.out.println(i);
System.out.println(count + "aa");


}
}
方法三(数组。。。待补)


你可能感兴趣的:(【java】【简单数学问题】如何实现1至6,实现一个不重复的4位数)