public class Naloga4 {
public static void main(String[] args) {
int mest = 0;
int stTekmovalcev = 2; // number of competitors a.k.a
//how long permutations should be
int N = 2*stTekmovalcev; // number of options, inluding disqualification of
//possibly every participant
int[] razvrstitve = new int[N];
for (int i = 0; i < N; i++) { // creates an array with all options inside
if (i < stTekmovalcev) {
razvrstitve[i] = 0;
} else {
razvrstitve[i] = mest+1;
mest++;
}
}
razvrsti(razvrstitve, razvrstitve.length, stTekmovalcev);
}
private static void razvrsti(int[] razvrstitve, int n, int r) { // prints out all possible permutations
if (r == 0) {
for (int i = n; i < razvrstitve.length; i++) {
String mesto = "";
if (razvrstitve[i] != 0) {
mesto = String.valueOf((int) razvrstitve) + ". mesto";
} else {
mesto = "odstop";
}
System.out.printf((i-n+1) + ". Voznik: " + mesto + " ");
}
System.out.println();
return;
}
for (int i = 0; i < n; i++) {
zamenjaj(razvrstitve, i, n-1);
razvrsti(razvrstitve, n-1, r-1);
zamenjaj(razvrstitve, i, n-1);
}
}
public static void zamenjaj(int[] razvrstitve, int i, int j) { // swaps positions in the array
// so all permutations can be made
int temp = razvrstitve[i];
razvrstitve[i] = razvrstitve[j];
razvrstitve[j] = temp;
}
}
public class ResultsPermutations {
public static void main(String[] args) {
int setResult = 0;
int nrOfCompetitors = 2; // number of competitors a.k.a
//how long permutations should be
int N = 2*nrOfCompetitors; // number of options, including disqualification of
// possibly every participant
int[] possibleresults = new int[N];
for (int i = 0; i < N; i++) { // creates an array with all possible results
if (i < nrOfCompetitors) {
possibleresults[i] = 0;
} else {
possibleresults[i] = setResult+1;
setResult++;
}
}
enumerate(possibleresults, possibleresults.length, nrOfCompetitors);
}
private static void enumerate(int[] possibleresults, int n, int r) { // prints out all possible permutations
if (r == 0) {
for (int i = n; i < possibleresults.length; i++) {
String place = "";
if (possibleresults[i] != 0) {
place = String.valueOf((int) possibleresults) + ". place";
} else {
place = "Disqualification";
}
System.out.printf("Driver " + "#" + (i-n+1) + ": " + place + " ");
}
System.out.println();
return;
}
for (int i = 0; i < n; i++) {
swapArray(possibleresults, i, n-1);
enumerate(possibleresults, n-1, r-1);
swapArray(possibleresults, i, n-1);
}
}
public static void swapArray(int[] possibleresults, int i, int j) { // swaps positions in the array
// so all permutations can be made
int temp = possibleresults[i];
possibleresults[i] = possibleresults[j];
possibleresults[j] = temp;
}
}
for (int i = 0; i < n; i++) {
output[r] = possibleresults;
if(i>0) {
swapArray(possibleresults, output, i, n-1);
enumerate(possibleresults, output, n-1, r-1);
swapArray(possibleresults, output, i, n-1);
}
else {
enumerate(possibleresults, output, n, r-1);
}
}The number of possible outputs grows in an interesting way. According to the On-Line Encyclopedia of Integer Sequences it equals the number of partial permutations of an n-set or the number of n X n binary matrices with at most one 1 in each row and column. Didn't know those have anything to do with driving races...
Slo_Sumo wrote:I can reproduce your result by making two bugs in my code: First, I set N = 2*nrOfCompetitors and second, I run the printing loop from n to output.length. Using the correct values (N = nrOfCompetitors+1 and running the printing loop from 1 to output.length) I get the desired seven outcomes.
Thanks for the input but changing that actually makes things even more messed up: