Ask the PHP algorithm to help me write an algorithm for a Football League.
The requirement is that there are six teams.
A1 A2 A3 A4 A5 A6
Then, if it's a round
First
A1VSA2 A3VSA4 A5VSA6
Second round
A1VSA4 A2VSA5 A3VSA6
Round 3
....
Each team plays 10 matches with the other five teams, including 5 home games and 5 away games (before and after him)
This algorithm is sad for me.
Finally, we need to follow a round. for example, if you select the second round, the second round will list the scores of the second round!
Reply to discussion (solution)
Is this a round robin or something? I forgot what to call it. What about the lottery? Or directly?
Given that there are both home and away games, the game is between two teams. one team is home, and the other team must be away. If it is specified, I think it can be as follows:
Set half of the teams, that is, three of them at home, and repeat with the remaining three teams.
Then try again.
Is this a round robin or something? I forgot what to call it. What about the lottery? Or directly?
Given that there are both home and away games, the game is between two teams. one team is home, and the other team must be away. If it is specified, I think it can be as follows:
Set half of the teams, that is, three of them at home, and repeat with the remaining three teams.
Then try again.
No, it seems wrong. it's too easy to think about...
?...
Hi, it's not that simple! Solution!
$a = array('A1', 'A2', 'A3', 'A4', 'A5', 'A6');berger_method($a);function berger_method($ar) { if(count($ar) %2) $ar[] = ' '; $t = array_merge(range(1, count($ar)-1), range(1, count($ar)-1)); $len = count($ar); $m = range(1, $len); $lun = 0; $last = 0; $k = $len <= 4 ? 1 : ($len - 4) / 2 + 1; while($lun++ < $len-1) { $s = array_values($m); echo "== $lun ==\n"; for($i=0; $i<$len/2; $i++) printf("%s -- %s\n", $ar[$s[$i]-1], $ar[$s[$len-1-$i]-1]); echo "\n"; list($m[0], $m[$len-1]) = array($m[$len-1], $m[0]); for($i=0; $i<$k; $i++) { if($m[++$last % $len] == $len) $last++; } $n = $last %= $len; for($i=1; $i<$len; $i++) { if(($m[$n]) == $len) $n = ($n + 1) % $len; $m[$n] = $i; $n = ($n + 1) % $len; } }}
== 1 ==A1 -- A6A2 -- A5A3 -- A4== 2 ==A6 -- A4A5 -- A3A1 -- A2== 3 ==A2 -- A6A3 -- A1A4 -- A5== 4 ==A6 -- A5A1 -- A4A2 -- A3== 5 ==A3 -- A6A4 -- A2A5 -- A1
This is a single loop
In a double loop
While ($ lun ++ <$ len-1 ){
Change
While ($ lun ++ <($ len-1) * 2 ){
You can.
It's really hard to get such an orchestration method for Berger
It took 5 or 6 hours to straighten out the algorithm.
For example, the first day A1-A2 A3-A4 A5-A6
The second wheel may be a A2-A1 A4-A3 A6-A5
That is, each team has two matches with its opponent.
But once at home!
Of course not that simple, otherwise the organizing committee is too easy
At present, the world round robin competition adopts the "Bergel orchestration method", which is my introduction. Of course, you can also use the "counter-clockwise rotation method". although there are some problems, the algorithm is much simpler.
I also searched for the arrangement method for the double round robin, but unfortunately I cannot find it.
So I did a test and found that the single-loop algorithm was extended to the dual-loop. The probability of success is only 36 in ten thousandths, which is why they do not want to publish the dual-loop orchestration method.
$ A = array ('A1 ', 'A2', 'A3 ', 'A4', 'a5 ', 'a6'); $ last = berger_method ($ ); set_time_limit (60); $ x = 10000; $ double = array (); do {shuffle ($ a); $ r = array_merge ($ last, berger_method ($ )); $ res = array_combine ($ a, array_fill (0, count ($ a), array ('number of play' => 0, 'home' => 0, 'Out' => 0); foreach ($ r as $ item) {$ res [$ item ['home'] ['number of play'] ++; $ res [$ item ['home'] ['home'] ++; $ res [$ item ['offline'] ['number of play'] ++; $ res [$ item ['offline'] ['offline' ] ++;} If (! Array_filter ($ res, function ($ v) {return $ v ['home']! = $ V ['out'] ;}) {$ double [] = join (',', $ a) ;}} while ($ x --); print_r (array_values (array_unique ($ double )));When the initial sequence is A1, A2, A3, A4, A5, A6
The initial sequence of the second single loop must be one of the following:
[0] => A4,A5,A6,A2,A3,A1 [1] => A5,A6,A4,A1,A2,A3 [2] => A5,A4,A6,A3,A2,A1 [3] => A4,A6,A5,A3,A1,A2 [4] => A5,A6,A4,A2,A3,A1 [5] => A6,A5,A4,A1,A2,A3 [6] => A6,A5,A4,A1,A3,A2 [7] => A6,A4,A5,A1,A2,A3 [8] => A4,A6,A5,A2,A3,A1 [9] => A5,A4,A6,A2,A3,A1 [10] => A6,A5,A4,A2,A1,A3 [11] => A5,A4,A6,A1,A2,A3 [12] => A4,A6,A5,A2,A1,A3 [13] => A4,A5,A6,A1,A2,A3 [14] => A6,A5,A4,A3,A2,A1 [15] => A5,A4,A6,A1,A3,A2 [16] => A6,A5,A4,A3,A1,A2 [17] => A4,A5,A6,A3,A2,A1 [18] => A6,A4,A5,A2,A1,A3 [19] => A4,A5,A6,A2,A1,A3 [20] => A5,A6,A4,A2,A1,A3 [21] => A6,A4,A5,A3,A2,A1 [22] => A5,A6,A4,A3,A1,A2 [23] => A4,A6,A5,A1,A2,A3 [24] => A5,A6,A4,A3,A2,A1 [25] => A4,A6,A5,A1,A3,A2 [26] => A6,A4,A5,A2,A3,A1 [27] => A5,A4,A6,A2,A1,A3 [28] => A4,A6,A5,A3,A2,A1 [29] => A4,A5,A6,A3,A1,A2 [30] => A5,A4,A6,A3,A1,A2 [31] => A6,A4,A5,A3,A1,A2 [32] => A6,A4,A5,A1,A3,A2 [33] => A5,A6,A4,A1,A3,A2 [34] => A6,A5,A4,A2,A3,A1 [35] => A4,A5,A6,A1,A3,A2
? Note.
Let's take a look!
The problem with the landlord is so interesting. let's play a table filling game! I suppose there are 6 fixed teams, with only one game and a small team number playing at home!
Want to play away? Let the team number play at home!
Should we have 10 games, five for each subject and object? Copy the two tables in the previous step five times!
There must be a problem with this simplicity.
Of course not that simple, otherwise the organizing committee is too easy
At present, the world round robin competition adopts the "Bergel orchestration method", which is my introduction. Of course, you can also use the "counter-clockwise rotation method". although there are some problems, the algorithm is much simpler.
I also searched for the arrangement method for the double round robin, but unfortunately I cannot find it.
So I did a test and found that the single-loop algorithm was extended to the dual-loop. The probability of success is only 36 in ten thousandths, which is why they do not want to publish the dual-loop orchestration method.
$ A = array ('A1 ', 'A2', 'A3 ', 'A4', 'a5 ', 'a6'); $ last = berger_method ($ ); set_time_limit (60); $ x = 10000; $ double = array (); do {shuffle ($ a); $ r = array_merge ($ last, berger_method ($ )); $ res = array_combine ($ a, array_fill (0, count ($ a), array ('number of play' => 0, 'home' => 0, 'Out' => 0); foreach ($ r as $ item) {$ res [$ item ['home'] ['number of play'] ++; $ res [$ item ['home'] ['home'] ++; $ res [$ item ['offline'] ['number of play'] ++; $ res [$ item ['offline'] ['offline' ] ++;} If (! Array_filter ($ res, function ($ v) {return $ v ['home']! = $ V ['out'] ;}) {$ double [] = join (',', $ a) ;}} while ($ x --); print_r (array_values (array_unique ($ double )));When the initial sequence is A1, A2, A3, A4, A5, A6
The initial sequence of the second single loop must be one of the following:
[0] => A4,A5,A6,A2,A3,A1 [1] => A5,A6,A4,A1,A2,A3 [2] => A5,A4,A6,A3,A2,A1 [3] => A4,A6,A5,A3,A1,A2 [4] => A5,A6,A4,A2,A3,A1 [5] => A6,A5,A4,A1,A2,A3 [6] => A6,A5,A4,A1,A3,A2 [7] => A6,A4,A5,A1,A2,A3 [8] => A4,A6,A5,A2,A3,A1 [9] => A5,A4,A6,A2,A3,A1 [10] => A6,A5,A4,A2,A1,A3 [11] => A5,A4,A6,A1,A2,A3 [12] => A4,A6,A5,A2,A1,A3 [13] => A4,A5,A6,A1,A2,A3 [14] => A6,A5,A4,A3,A2,A1 [15] => A5,A4,A6,A1,A3,A2 [16] => A6,A5,A4,A3,A1,A2 [17] => A4,A5,A6,A3,A2,A1 [18] => A6,A4,A5,A2,A1,A3 [19] => A4,A5,A6,A2,A1,A3 [20] => A5,A6,A4,A2,A1,A3 [21] => A6,A4,A5,A3,A2,A1 [22] => A5,A6,A4,A3,A1,A2 [23] => A4,A6,A5,A1,A2,A3 [24] => A5,A6,A4,A3,A2,A1 [25] => A4,A6,A5,A1,A3,A2 [26] => A6,A4,A5,A2,A3,A1 [27] => A5,A4,A6,A2,A1,A3 [28] => A4,A6,A5,A3,A2,A1 [29] => A4,A5,A6,A3,A1,A2 [30] => A5,A4,A6,A3,A1,A2 [31] => A6,A4,A5,A3,A1,A2 [32] => A6,A4,A5,A1,A3,A2 [33] => A5,A6,A4,A1,A3,A2 [34] => A6,A5,A4,A2,A3,A1 [35] => A4,A5,A6,A1,A3,A2
Well, I think it may be right but there may be problems! I'm still testing!
The problem with the landlord is so interesting. let's play a table filling game! I suppose there are 6 fixed teams, with only one game and a small team number playing at home!
Want to play away? Let the team number play at home!
Should we have 10 games, five for each subject and object? Copy the two tables in the previous step five times!
There must be a problem with this simplicity.
The key is that if the team is 12, it's hard to write a live algorithm.
I already gave you the answer.
You only need to change the order of the teams in the next but loop.
It's always meaningless to tangle with such trivial matters.
$a = array('A1', 'A2', 'A3', 'A4', 'A5', 'A6');berger_method($a);function berger_method($ar) { if(count($ar) %2) $ar[] = ' '; $t = array_merge(range(1, count($ar)-1), range(1, count($ar)-1)); $len = count($ar); $m = range(1, $len); $lun = 0; $last = 0; $k = $len <= 4 ? 1 : ($len - 4) / 2 + 1; while($lun++ < $len-1) { $s = array_values($m); echo "== $lun ==\n"; for($i=0; $i<$len/2; $i++) printf("%s -- %s\n", $ar[$s[$i]-1], $ar[$s[$len-1-$i]-1]); echo "\n"; list($m[0], $m[$len-1]) = array($m[$len-1], $m[0]); for($i=0; $i<$k; $i++) { if($m[++$last % $len] == $len) $last++; } $n = $last %= $len; for($i=1; $i<$len; $i++) { if(($m[$n]) == $len) $n = ($n + 1) % $len; $m[$n] = $i; $n = ($n + 1) % $len; } }}
== 1 ==A1 -- A6A2 -- A5A3 -- A4== 2 ==A6 -- A4A5 -- A3A1 -- A2== 3 ==A2 -- A6A3 -- A1A4 -- A5== 4 ==A6 -- A5A1 -- A4A2 -- A3== 5 ==A3 -- A6A4 -- A2A5 -- A1
This is a single loop
In a double loop
While ($ lun ++ <$ len-1 ){
Change
While ($ lun ++ <($ len-1) * 2 ){
You can.
It's really hard to get such an orchestration method for Berger
It took 5 or 6 hours to straighten out the algorithm.
Haha, good, very patient.
I had a hard time and found that it was not that simple and busy, so I didn't study it any more.
Algorithms are the soul of software.