例如,Java数组a[1,2,3,4,5,6],随机选择3个位置1,3,5.交换它们的位置,如何得到所有交换后的数组
交换后的[1,4,3,2,5,6],[1,4,3,6,5,2],[1,6,3,2,5,4],[1,6,3,4,5,2]...
交换后的[1,4,3,2,5,6],[1,4,3,6,5,2],[1,6,3,2,5,4],[1,6,3,4,5,2]
展开
2个回答
展开全部
import java.util.Arrays;
public class BaiduZhidao {
/**
* @param args the command line arguments
*/
public int[] randomCommon(int min, int max, int n) {
if (n > (max - min + 1) || max < min) {
return null;
}
int[] result = new int[n];
int count = 0;
while (count < n) {
int num = (int) (Math.random() * (max - min)) + min;
boolean flag = true;
for (int j = 0; j < n; j++) {
if (num == result[j]) {
flag = false;
break;
}
}
if (flag) {
result[count] = num;
count++;
}
}
return result;
}
public static void main(String[] args) {
int a[] = {1, 2, 3, 4, 5, 6};
int index[] = new int[3];
index = new BaiduZhidao().randomCommon(0, 7, 3);
int r1[] = new int[6];
int r2[] = new int[6];
int r3[] = new int[6];
int r4[] = new int[6];
for (int i = 0; i < index.length; i++) {
index[i] = index[i] - 1;
}
Arrays.sort(index);
r1 = Arrays.copyOf(a, a.length);
r2 = Arrays.copyOf(a, a.length);
r3 = Arrays.copyOf(a, a.length);
r4 = Arrays.copyOf(a, a.length);
r1[index[0]] = a[index[1]];
r1[index[1]] = a[index[0]];
r1[index[2]] = a[index[2]];
r2[index[0]] = a[index[1]];
r2[index[1]] = a[index[2]];
r2[index[2]] = a[index[0]];
r3[index[0]] = a[index[2]];
r3[index[1]] = a[index[0]];
r3[index[2]] = a[index[1]];
r4[index[0]] = a[index[2]];
r4[index[1]] = a[index[1]];
r4[index[2]] = a[index[0]];
for (int i = 0; i < r1.length; i++) {
System.out.print(r1[i] + " ");
}
System.out.println();
for (int i = 0; i < r1.length; i++) {
System.out.print(r2[i] + " ");
}
System.out.println();
for (int i = 0; i < r1.length; i++) {
System.out.print(r3[i] + " ");
}
System.out.println();
for (int i = 0; i < r1.length; i++) {
System.out.print(r4[i] + " ");
}
}
}
展开全部
最笨的方法...
package com.softeem.exchangeposition;
public class ChangePosition {
public void printInts(int[] a){
for (int i =0;i<a.length;i++) {
System.out.print(a[i]);
}
System.out.println();
}
public void swap(int[] a,int m,int n){
int temp;
temp =a[m-1];
a[m-1] = a[n-1];
a[n-1] = temp;
}
public void exchangeByPositionNo(int[] a,int m,int n,int j){
swap(a,m,n);
printInts(a);
swap(a,m,j);
printInts(a);
swap(a,n,m);
printInts(a);
swap(a,n,j);
printInts(a);
swap(a,j,m);
printInts(a);
swap(a,j,n);
printInts(a);
}
public static void main(String[] args) {
int a[] = {1,2,3,4,5};
ChangePosition cp =new ChangePosition();
cp.exchangeByPositionNo(a, 1, 3, 5);
}
}
package com.softeem.exchangeposition;
public class ChangePosition {
public void printInts(int[] a){
for (int i =0;i<a.length;i++) {
System.out.print(a[i]);
}
System.out.println();
}
public void swap(int[] a,int m,int n){
int temp;
temp =a[m-1];
a[m-1] = a[n-1];
a[n-1] = temp;
}
public void exchangeByPositionNo(int[] a,int m,int n,int j){
swap(a,m,n);
printInts(a);
swap(a,m,j);
printInts(a);
swap(a,n,m);
printInts(a);
swap(a,n,j);
printInts(a);
swap(a,j,m);
printInts(a);
swap(a,j,n);
printInts(a);
}
public static void main(String[] args) {
int a[] = {1,2,3,4,5};
ChangePosition cp =new ChangePosition();
cp.exchangeByPositionNo(a, 1, 3, 5);
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询