0
数字の1から10までのランダムな順列を生成するプログラムを作成する必要があります。可能であれば、メソッドや追加ライブラリはありません。できるだけ単純です。ここまで私がこれまで持っていたことがあります。2番目の配列はランダムです
import java.util.Scanner;
public class SwitchingNum {
public static void main(String[] args) {
int num = 10;
Scanner in = new Scanner(System.in);
int[] tempArray = new int[10];
int currentSize = tempArray.length;
System.out.print("First Array: ");
for(int i = 0; i < num; i++){
tempArray[i] = i + 1;
System.out.print(tempArray[i] + " ");
}
int[] permArray = new int[tempArray.length];
System.out.println();
System.out.print("Second Array: ");
for (int i = 0; i < permArray.length; i ++){
permArray[i] = tempArray[(int) (Math.random() * currentSize -1)];
for (int j = i; j < currentSize -1; j++){
tempArray[i] = tempArray[i+1];
}
currentSize--;
System.out.print(permArray[i] + " ");
}
}
}
ここにあなたが必要なものを説明する役に立つビデオがあります:https://youtu.be/U0nvXHh7o-w?t=4m36s – Ain
あなたの現在の解は一様ではなく、O(n^2)です。 – Ain
[Generate an uniform random permutation]の可能な複製(http://stackoverflow.com/questions/7902391/generate-an-uniform-random-permutation) – Leon