Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 求PHP算法 求大神帮助

求PHP算法 求大神帮助

Jun 23, 2016 pm 01:51 PM
php Great God algorithm

我有一个需求 写一个 足球联赛的 算法。
需求是这样的 很简单 有六只球队
A1 A2 A3 A4 A5 A6 
然后 如果一轮一场比赛的话
  第一论
  A1VSA2    A3VSA4   A5VSA6
第二轮
A1VSA4   A2VSA5  A3VSA6
 第三轮
....

每个球队都会跟 其他五个球队比赛10次   其中 主场5次  客场5次  (他在前和 在后)

这么个算法 愁死我了 

最后就要 按照 一轮一场    如 让用户选择  第二轮  就会 列出 第二轮  谁和谁的比分来!  


回复讨论(解决方案)

这是循环赛还是什么么?忘了怎么称呼了。排赛是抽签呢?还是直接指定?
鉴于有主场和客场,同时,比赛是两队之间的,一队是主场,另一队必定是客场。如果是指定的话,我觉得可以这样来:
先定一半球队即其中三个球队为主场,跟剩下的三队循环一下。
然后反一下,再来一次。

这是循环赛还是什么么?忘了怎么称呼了。排赛是抽签呢?还是直接指定?
鉴于有主场和客场,同时,比赛是两队之间的,一队是主场,另一队必定是客场。如果是指定的话,我觉得可以这样来:
先定一半球队即其中三个球队为主场,跟剩下的三队循环一下。
然后反一下,再来一次。



不对,好像搞错了,想太简单了。。。
?。。。

你好  不是那么简单的!求解!

$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;    }  }}
Copy after login
Copy after login
== 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
Copy after login
Copy after login
这是单循环的
双循环将 14 行的
while($lun++ < $len-1) {
改作
while($lun++ < ($len-1)*2) {
就可以了

真难为了 贝格尔 弄出这么个编排法
绕了5、6个小时才把算法理顺

比如 第一天 A1-A2 A3-A4 A5-A6
第二轮 可能是A2-A1 A4-A3 A6-A5
就是每个队 跟对手 都有两场比赛
但是 主场一次 客场一次!

当然不是那么简单,否则赛事组委会也太轻松了
目前世界性单循环赛都采用“贝格尔编排方法”,就是我介绍的这种。当然你也可以采用“逆时针轮转方法”,虽然存在一些问题,但算法要简单的多

我也搜索了双循环赛的编排方法,但遗憾的是无法找到。
于是我做了一下测试,结果发现把单循环的算法推广到双循环时。成功的概率只有万分之36,这也就是都不愿公开双循环编排方法的原因吧。

$a = array('A1', 'A2', 'A3', 'A4', 'A5', 'A6');$last = berger_method($a);set_time_limit(60);$x = 10000;$double = array();do {  shuffle($a);  $r = array_merge($last, berger_method($a));  $res = array_combine($a, array_fill(0, count($a), array('场数' => 0, '主场' => 0, '客场' => 0)));  foreach($r as $item) {    $res[$item['主场']]['场数']++;    $res[$item['主场']]['主场']++;    $res[$item['客场']]['场数']++;    $res[$item['客场']]['客场']++;  }  if(! array_filter($res, function($v) { return $v['主场'] != $v['客场']; })) {    $double[] = join(',', $a);  }}while($x--);print_r(array_values(array_unique($double)));
Copy after login
Copy after login
可知,当初始序列为 A1,A2,A3,A4,A5,A6 时
第二个单循环的初始序列必须为下列之一时,才能要求
    [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
Copy after login
Copy after login

?注一下。

来看看!

楼主的问题太有趣了,让我们来玩个填表游戏吧!我假设固定6只球队,只打一场且队伍号码小的占主场!











要打客场?让队号大的打主场就OK!
要打10场,主客各5场?把上一步的2个表复制5遍!

这么简单肯定有问题

当然不是那么简单,否则赛事组委会也太轻松了
目前世界性单循环赛都采用“贝格尔编排方法”,就是我介绍的这种。当然你也可以采用“逆时针轮转方法”,虽然存在一些问题,但算法要简单的多

我也搜索了双循环赛的编排方法,但遗憾的是无法找到。
于是我做了一下测试,结果发现把单循环的算法推广到双循环时。成功的概率只有万分之36,这也就是都不愿公开双循环编排方法的原因吧。

$a = array('A1', 'A2', 'A3', 'A4', 'A5', 'A6');$last = berger_method($a);set_time_limit(60);$x = 10000;$double = array();do {  shuffle($a);  $r = array_merge($last, berger_method($a));  $res = array_combine($a, array_fill(0, count($a), array('场数' => 0, '主场' => 0, '客场' => 0)));  foreach($r as $item) {    $res[$item['主场']]['场数']++;    $res[$item['主场']]['主场']++;    $res[$item['客场']]['场数']++;    $res[$item['客场']]['客场']++;  }  if(! array_filter($res, function($v) { return $v['主场'] != $v['客场']; })) {    $double[] = join(',', $a);  }}while($x--);print_r(array_values(array_unique($double)));
Copy after login
Copy after login
可知,当初始序列为 A1,A2,A3,A4,A5,A6 时
第二个单循环的初始序列必须为下列之一时,才能要求
    [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
Copy after login
Copy after login



嗯 但是 我感觉 将赛场 反算 可能就对了 但是 可能也有问题! 我还在测试!



楼主的问题太有趣了,让我们来玩个填表游戏吧!我假设固定6只球队,只打一场且队伍号码小的占主场!











要打客场?让队号大的打主场就OK!
要打10场,主客各5场?把上一步的2个表复制5遍!

这么简单肯定有问题



关键是 如果 球队是12个呢 是个活的 算法比较难写

答案我已经给你了
只要在下一个但循环的时候变化一下参赛队的次序就可以了
总纠结这种小事的没有意义的

$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;    }  }}
Copy after login
Copy after login
== 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
Copy after login
Copy after login
这是单循环的
双循环将 14 行的
while($lun++  改作
while($lun++  就可以了

真难为了 贝格尔 弄出这么个编排法
绕了5、6个小时才把算法理顺


哈哈,不错,挺有耐心的。
我折腾了下,发现没那么简单,正好也忙,就没再去研究了。

算法才是软件的灵魂呀

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

See all articles