Ask the PHP algorithm for help from the great God

Source: Internet
Author: User
Tags shuffle
I have a need to write an algorithm for a Football League.
The demand is so simple there are six teams
A1 A2 A3 A4 A5 A6
And then, if it's a game,
First theory
A1VSA2 A3VSA4 A5VSA6
Second round
A1VSA4 A2vsa5 A3VSA6
Third round
....

Each team will compete with five other teams 10 times at home 5 times away 5 times (he is in front and behind)

This algorithm is killing me.

In the end, follow a round of the second round as the user chooses to list the second round who and whose score comes!


Reply to discussion (solution)

Is this a round robin or what? Forgot what to call it. What about the lottery? Or is it a direct designation?
In view of home and away, at the same time, the game is between the two teams, the first team is home, the other team must be away. If it is specified, I think this can be done:
The first half of the team is three teams for the home, with the remaining three teams to cycle.
Then reverse it and try again.

Is this a round robin or what? Forgot what to call it. What about the lottery? Or is it a direct designation?
In view of home and away, at the same time, the game is between the two teams, the first team is home, the other team must be away. If it is specified, I think this can be done:
The first half of the team is three teams for the home, with the remaining three teams to cycle.
Then reverse it and try again.



No, it seems to be wrong, it's too easy ...
?。。。

You are not so simple! Solving!

 $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-$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.
The double loop will be 14 rows of
while ($lun + + < $len-1) {
Change for
while ($lun + < ($len-1) *) {
You can do it.

It's hard for beagle to make this arrangement.
Around 5 or 6 hours to straighten out the algorithm.

Like the first day a1-a2 a3-a4 a5-a6
The second round may be a2-a1 a4-a3 a6-a5
Is that every team has two games with his opponent.
But home one time away!

Of course it's not that simple, otherwise the tournament organizing committee is too easy.
At present, the world single round robin has adopted the "Beagle Arrangement method", is I introduce this. Of course you can also use the "anti-clockwise rotation method", although there are some problems, but the algorithm is much simpler

I also searched for the arrangement of the double round robin, but unfortunately couldn't find it.
So I did a bit of testing and found that the single-loop algorithm was extended to double loops. The probability of success is only 36, which is why you are reluctant to open the double-loop orchestration method.

$a = Array (' A1 ', ' A2 ', ' A3 ', ' A4 ', ' A5 ', ' A6 '); $last = Berger_method ($a); set_time_limit; $x = 10000; $double = Array ();d o {  shuffle ($a);  $r = Array_merge ($last, Berger_method ($a));  $res = Array_combine ($a, Array_fill (0, Count ($a), Array (' field count ' = + 0, ' home ' = 0, ' away ' = 0));  foreach ($r as $item) {    $res [$item [' Home ']][' field number ']++;    $res [$item [' Home ']][' home ']++;    $res [$item [' Away ']][' field number ']++;    $res [$item [' Away ']][' away ']++;  }  if (! Array_filter ($res, function ($v) {return $v [' home ']! = $v [' Away '];})) {    $double [] = Join (', ', $a);  }} while ($x-);p Rint_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 to require
    [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,a 2,A3 [8] = A4,A6,A5,A2,A3,A1 [9] = A5,A4,A6,A2,A3,A1 [Ten] = a6,a5,a4,a2,a1,a3 [one] = A5,a4,a6, A1,A2,A3 [[] = a4,a6,a5,a2,a1,a3 [] = a4,a5,a6,a1,a2,a3 [+] = a6,a5,a4,a3,a2,a1 [] = A5, A4,A6,A1,A3,A2 [+] = a6,a5,a4,a3,a1,a2 [+] = a4,a5,a6,a3,a2,a1 [+] = a6,a4,a5,a2,a1,a3 [+] =&G T A4,A5,A6,A2,A1,A3 [+] = a5,a6,a4,a2,a1,a3 [23] [+] = a6,a4,a5,a3,a2,a1 [] = A5,A6,A4,A3,A1,A2    = A4,a6,a5,a1,a2,a3 [[] = A5,A6,A4,A3,A2,A1 [+] = a4,a6,a5,a1,a3,a2 [+] = A6,A4,A5,A2,A3,A1 [+] = a5,a4,a6,a2,a1,a3 [] = A4,A6,A5,A3,A2,A1 [+] = a4,a5,a6,a3,a1,a2 [+] = A5,A4,A6,A3,A1 , A2 [] = A6,A4,A5,A3,A1,A2 [[+] = a6,a4,a5,a1,a3,a2 [] = A5,A6,A4,A1,A3,A2 [+] = a6,a5,a4,a2,a3,a1 [=&G] T A4,a5,a6,a1,a3,a2

? Note.

Look at it!

Landlord's question is too interesting, let's play a fill-up game! I assume 6 teams are fixed, only one game and the team number is small to occupy the home!











Want to play away? Let's play the big team, ok!.
To play 10 games, 5 games for the subject? Copy the 2 tables from the previous step 5 times!

There must be something so simple.

Of course it's not that simple, otherwise the tournament organizing committee is too easy.
At present, the world single round robin has adopted the "Beagle Arrangement method", is I introduce this. Of course you can also use the "anti-clockwise rotation method", although there are some problems, but the algorithm is much simpler

I also searched for the arrangement of the double round robin, but unfortunately couldn't find it.
So I did a bit of testing and found that the single-loop algorithm was extended to double loops. The probability of success is only 36, which is why you are reluctant to open the double-loop orchestration method.

$a = Array (' A1 ', ' A2 ', ' A3 ', ' A4 ', ' A5 ', ' A6 '); $last = Berger_method ($a); set_time_limit; $x = 10000; $double = Array ();d o {  shuffle ($a);  $r = Array_merge ($last, Berger_method ($a));  $res = Array_combine ($a, Array_fill (0, Count ($a), Array (' field count ' = + 0, ' home ' = 0, ' away ' = 0));  foreach ($r as $item) {    $res [$item [' Home ']][' field number ']++;    $res [$item [' Home ']][' home ']++;    $res [$item [' Away ']][' field number ']++;    $res [$item [' Away ']][' away ']++;  }  if (! Array_filter ($res, function ($v) {return $v [' home ']! = $v [' Away '];})) {    $double [] = Join (', ', $a);  }} while ($x-);p Rint_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 to require
    [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,a 2,A3 [8] = A4,A6,A5,A2,A3,A1 [9] = A5,A4,A6,A2,A3,A1 [Ten] = a6,a5,a4,a2,a1,a3 [one] = A5,a4,a6, A1,A2,A3 [[] = a4,a6,a5,a2,a1,a3 [] = a4,a5,a6,a1,a2,a3 [+] = a6,a5,a4,a3,a2,a1 [] = A5, A4,A6,A1,A3,A2 [+] = a6,a5,a4,a3,a1,a2 [+] = a4,a5,a6,a3,a2,a1 [+] = a6,a4,a5,a2,a1,a3 [+] =&G T A4,A5,A6,A2,A1,A3 [+] = a5,a6,a4,a2,a1,a3 [23] [+] = a6,a4,a5,a3,a2,a1 [] = A5,A6,A4,A3,A1,A2    = A4,a6,a5,a1,a2,a3 [[] = A5,A6,A4,A3,A2,A1 [+] = a4,a6,a5,a1,a3,a2 [+] = A6,A4,A5,A2,A3,A1 [+] = a5,a4,a6,a2,a1,a3 [] = A4,A6,A5,A3,A2,A1 [+] = a4,a5,a6,a3,a1,a2 [+] = A5,A4,A6,A3,A1 , A2 [] = A6,A4,A5,A3,A1,A2 [[+] = a6,a4,a5,a1,a3,a2 [] = A5,A6,A4,A1,A3,A2 [+] = a6,a5,a4,a2,a3,a1 [=&G] T A4,a5,a6,a1,a3,a2



Yes, but I think it might be right to reverse the game, but there may be problems too! I'm still in the test!



Landlord's question is too interesting, let's play a fill-up game! I assume 6 teams are fixed, only one game and the team number is small to occupy the home!











Want to play away? Let's play the big team, ok!.
To play 10 games, 5 games for the subject? Copy the 2 tables from the previous step 5 times!

There must be something so simple.



The point is, if the team is 12, it's a live algorithm, it's hard to write.

I already gave you the answer.
Just change the order of the teams in the next but the loop.
There's no point in obsessing about this little thing.

 $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-$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.
The double loop will be 14 rows of
while ($lun + + < $len-1) {
Change for
while ($lun + < ($len-1) *) {
You can do it.

It's hard for beagle to make this arrangement.
Around 5 or 6 hours to straighten out the algorithm.


Haha, good, very patient.
I toss the next, found not so simple, just too busy, and did not go to study.

The algorithm is the soul of software

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.