package com;public class BuildData { public static void main(String[] args) { int i, j, k; int d =0; // 以下为三重循环 for (i = 1; i < 5; i++) { for (j = 1; j < 5; j++) { for (k = 1; k < 5; k++) { if (i != k && i != j && j != k) { System.out.println(i*100+j*10+k); d++; }
} } } System.out.println("总共---"+d+"--个"); } }
int[] num = new int[4]; num[1] = 1;...num[4] = 4; int count = 0; for(int i = 0 ; i < 4 ; i ++){ for(int j = 0; j < 4; j++){ if(i == j){ continue; } for(int k = 0; k < 4 ; k++){ if(i == k || j== k ){ continue; }else{ System.out.print(num[i]*100+num[j]*10+num[k]); count++; } } } }
public class Tester { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int[] a = { 1, 2, 3, 4}; int first = 0; int second = 0; int third = 0; int i; int j = 0; int k = 0; for (i = 0; i < a.length; i++) { first = a[i]; while (j < a.length) { if (j == i) { j++; } else { second = a[j]; while (k < a.length) { if (k == i || k == j) { k++; } else { third = a[k]; System.out.println(first * 100 + second * 10 + third); k++; } } j++; k=0; } } j=0; k=0; } } } 结果: 123 124 132 134 142 143 213 214 231 234 241 243 312 314 321 324 341 342 412 413 421 423 431 432
看楼主没结贴,我也来写一个。楼上的办法当然都很好,但是如果这几个数不是连续的,或者不是数字可能上面的方法就有点困难了。import java.util.*;public class AllSort{ static int count = 0; static char[] buf = {'1', '2', '3', '4'}; static ArrayList<String> list = new ArrayList<String>();
public static void main(String[] args) { select(buf, list, 3);
int i, j, k;
int d =0;
// 以下为三重循环
for (i = 1; i < 5; i++) {
for (j = 1; j < 5; j++) {
for (k = 1; k < 5; k++) {
if (i != k && i != j && j != k) {
System.out.println(i*100+j*10+k);
d++;
}
} } }
System.out.println("总共---"+d+"--个"); }
}
int count = 0;
for(int i = 0 ; i < 4 ; i ++){
for(int j = 0; j < 4; j++){
if(i == j){
continue;
}
for(int k = 0; k < 4 ; k++){
if(i == k || j== k ){
continue;
}else{
System.out.print(num[i]*100+num[j]*10+num[k]);
count++;
}
}
}
}
public class Tester { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = { 1, 2, 3, 4};
int first = 0;
int second = 0;
int third = 0;
int i;
int j = 0;
int k = 0;
for (i = 0; i < a.length; i++) {
first = a[i];
while (j < a.length) {
if (j == i) {
j++;
} else {
second = a[j];
while (k < a.length) {
if (k == i || k == j) {
k++;
} else {
third = a[k];
System.out.println(first * 100 + second * 10
+ third);
k++;
}
}
j++;
k=0;
}
}
j=0;
k=0;
}
}
}
结果:
123
124
132
134
142
143
213
214
231
234
241
243
312
314
321
324
341
342
412
413
421
423
431
432
static int count = 0;
static char[] buf = {'1', '2', '3', '4'};
static ArrayList<String> list = new ArrayList<String>();
public static void main(String[] args) {
select(buf, list, 3);
for(String str : list){
char[] temp = str.toCharArray();
perm(temp,0,temp.length-1);
}
System.out.println("In total: "+ count);
}
public static void select(char[] source, ArrayList<String> arrayList, int num){ //从4个数字钟选取3个
int l = source.length;
char[] temp = new char[num];
System.arraycopy(source, 0, temp, 0, num);
arrayList.add(new String(temp));
for(int i=num; i<l; i++){
for (int j=0; j<num; j++){
char tempChar = temp[j];
temp[j] = source[i];
arrayList.add(new String(temp));
temp[j] = tempChar;
}
}
}
public static void perm(char[] buf, int start, int end){
if(start==end){//当只要求对数组中一个字母进行全排列时,只要就按该数组输出即可
for(int i=0;i<=end;i++){
System.out.print(buf[i]);
}
count ++;
System.out.println();
}
else{//多个字母全排列
for(int i=start;i<=end;i++){
char temp=buf[start];//交换数组第一个元素与后续的元素
buf[start]=buf[i];
buf[i]=temp;
perm(buf,start+1,end);//后续元素递归全排列
temp=buf[start];//将交换后的数组还原
buf[start]=buf[i];
buf[i]=temp;
}
}
}
}