# All possible race outcomes

**843853**Apr 6, 2010 5:13 AM

Hey,

This is my problem - I'm making a program that will print all possible outcomes of a race, given that ALL participants can be disqualified, and logically, that there can not be several same places.

Say that we have 2 competitors. My idea was/is to put all the options into an array razvrstitve = (1st place, 2nd place, disqualification, disqualification), and then print all the possible permutations of it. Problem is that it also considers all disqualifications to be separate, so it, will for example print out that "1st driver : disqualified (taking in razvrstitve[2]), 2nd driver: disqualified (razvrstitve[3])" and "1st driver : disqualified (taking in razvrstitve[3]), 2nd driver: disqualified (razvrstitve[2])", which is practically the same and shouldn't be printed out.

Output example(odstop means disqualification):

1. Voznik: 2. mesto 2. Voznik: odstop

1. Voznik: odstop 2. Voznik: odstop

1. Voznik: 1. mesto 2. Voznik: odstop

1. Voznik: odstop 2. Voznik: odstop

1. Voznik: 2. mesto 2. Voznik: odstop

1. Voznik: 1. mesto 2. Voznik: odstop

1. Voznik: odstop 2. Voznik: 1. mesto

1. Voznik: odstop 2. Voznik: 1. mesto

1. Voznik: 2. mesto 2. Voznik: 1. mesto

1. Voznik: odstop 2. Voznik: 2. mesto

1. Voznik: odstop 2. Voznik: 2. mesto

1. Voznik: 1. mesto 2. Voznik: 2. mesto

I've tried with creating new arrays and not "letting in" those elements, which's exact order has already been printed, but It's not working like it should :/

Anyway, here is my code (the one that prints out all permutations, including duplicates), so if anyone has any ideas or pointers of how I can do what I want it'd be greatly appreciated.

Regards,

Sumo

This is my problem - I'm making a program that will print all possible outcomes of a race, given that ALL participants can be disqualified, and logically, that there can not be several same places.

Say that we have 2 competitors. My idea was/is to put all the options into an array razvrstitve = (1st place, 2nd place, disqualification, disqualification), and then print all the possible permutations of it. Problem is that it also considers all disqualifications to be separate, so it, will for example print out that "1st driver : disqualified (taking in razvrstitve[2]), 2nd driver: disqualified (razvrstitve[3])" and "1st driver : disqualified (taking in razvrstitve[3]), 2nd driver: disqualified (razvrstitve[2])", which is practically the same and shouldn't be printed out.

Output example(odstop means disqualification):

1. Voznik: 2. mesto 2. Voznik: odstop

1. Voznik: odstop 2. Voznik: odstop

1. Voznik: 1. mesto 2. Voznik: odstop

1. Voznik: odstop 2. Voznik: odstop

1. Voznik: 2. mesto 2. Voznik: odstop

1. Voznik: 1. mesto 2. Voznik: odstop

1. Voznik: odstop 2. Voznik: 1. mesto

1. Voznik: odstop 2. Voznik: 1. mesto

1. Voznik: 2. mesto 2. Voznik: 1. mesto

1. Voznik: odstop 2. Voznik: 2. mesto

1. Voznik: odstop 2. Voznik: 2. mesto

1. Voznik: 1. mesto 2. Voznik: 2. mesto

I've tried with creating new arrays and not "letting in" those elements, which's exact order has already been printed, but It's not working like it should :/

Anyway, here is my code (the one that prints out all permutations, including duplicates), so if anyone has any ideas or pointers of how I can do what I want it'd be greatly appreciated.

Regards,

Sumo

```
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;

}

}` `

- 1583 Views
- Tags: none (add)