Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 新人请教一个该如何写*算法

新人请教一个该如何写*算法

Jun 23, 2016 pm 02:01 PM

主要原因:

目前在学习PHP的过程,想考验一下自己制作一个足球比赛单循环的赛程表出来。

算法:

*算法
把队伍排列成2行(偶数队伍),固定第一行的第一个队伍,从第一行第二个队伍开始顺时针滚动。

但我在写代码的时候怎么也写不出来(可能是我也刚接触)

我所设想的写法应该是:

3层for循环,第一层是轮次,第二层是主队,第三层是客队。
进行for echo展示赛程表。

下面代码是我自己在研究过程中写出来的,跟我要的结果完全不一样啊,不知道怎么写了,希望PHP大牛帮忙!

1

<?php    $team = array('武汉','广州','北京','上海','四川','香港','台湾');  //var_dump($team);  $max = count($team);    //var_dump($max);   $teammax = $max - 1 ;   for ($i=0;$i<$max;$i++)  {       for ($o=0;$o<=$teammax;$o++)     {           if ($i == $o)           {               continue;           }               else            {           echo $team[$i] . ' vs ' . $team[$o];            if ($o == $teammax){                echo "<hr>";          }           else{           echo "<br/>";     }   }       }   }?>

Copy after login


1

<?php    $team = array('1','2','3','4','5','6','7','8'); $max = count($team);    //var_dump($max);   $lunci = $max - 1;  //var_dump($lunci); for ($i=0;$i<$lunci;$i++)    {       echo $team[$w] .' vs ' . $team[$b-$i];      echo '<br/>'; }?>

Copy after login


回复讨论(解决方案)

你这是求 7 中取 2 的组合
按求组合的通用算法即可(精华区有相关代码)

规则看不太懂 能给个结果实例要你想要的结果展示出来吗 这样别人才能更好的知道你想要的结果是什么

自己研究的话,可以这样写

1

$team = array('武汉','广州','北京','上海','四川','香港','台湾');$max = count($team);$teammax = $max - 1 ;for ($i=0;$i<$max;$i++) {  for ($o=$i+1;$o<=$teammax;$o++) {    if ($i == $o) {      continue;    }else {      echo $team[$i] . ' vs ' . $team[$o];      if ($o == $teammax){        echo "<hr>";      } else{        echo "<br/>";      }    }  }}

Copy after login
Copy after login
武汉 vs 广州
武汉 vs 北京
武汉 vs 上海
武汉 vs 四川
武汉 vs 香港
武汉 vs 台湾
--------------------------------------------------------------------------------
广州 vs 北京
广州 vs 上海
广州 vs 四川
广州 vs 香港
广州 vs 台湾
--------------------------------------------------------------------------------
北京 vs 上海
北京 vs 四川
北京 vs 香港
北京 vs 台湾
--------------------------------------------------------------------------------
上海 vs 四川
上海 vs 香港
上海 vs 台湾
--------------------------------------------------------------------------------
四川 vs 香港
四川 vs 台湾
--------------------------------------------------------------------------------
香港 vs 台湾
--------------------------------------------------------------------------------

规则看不太懂 能给个结果实例要你想要的结果展示出来吗 这样别人才能更好的知道你想要的结果是什么

实力结果应该是
第一轮
也就是把
1234
5678
上下对阵
1523
6784

1652
7843

形成罗盘算法的运转(固定1 转动2~8)

自己研究的话,可以这样写

1

$team = array('武汉','广州','北京','上海','四川','香港','台湾');$max = count($team);$teammax = $max - 1 ;for ($i=0;$i<$max;$i++) {  for ($o=$i+1;$o<=$teammax;$o++) {    if ($i == $o) {      continue;    }else {      echo $team[$i] . ' vs ' . $team[$o];      if ($o == $teammax){        echo "<hr>";      } else{        echo "<br/>";      }    }  }}

Copy after login
Copy after login
武汉 vs 广州
武汉 vs 北京
武汉 vs 上海
武汉 vs 四川
武汉 vs 香港
武汉 vs 台湾
--------------------------------------------------------------------------------
广州 vs 北京
广州 vs 上海
广州 vs 四川
广州 vs 香港
广州 vs 台湾
--------------------------------------------------------------------------------
北京 vs 上海
北京 vs 四川
北京 vs 香港
北京 vs 台湾
--------------------------------------------------------------------------------
上海 vs 四川
上海 vs 香港
上海 vs 台湾
--------------------------------------------------------------------------------
四川 vs 香港
四川 vs 台湾
--------------------------------------------------------------------------------
香港 vs 台湾
--------------------------------------------------------------------------------

你好,版主,你这样的写法我写过,最终生成的结果和我想要的结果是不一样的。

我所想的是

第一轮
1234
5678
第二轮
1523
6784
第三轮
1652
7843
以此类推这样的算法0.0

$arr = array(4,3,2,1,5,6,7,8);
$round = 0; //设置轮次
$arr1 = array_merge($arr, $arr);
$round %= 8;
echo $arr1[$round],$arr1[$round+1],$arr1[$round+2],$arr1[$round+3];

这个意思?

1

$ar = array(1, 2, 3, 4, 5, 6, 7, 8);$len = count($ar);for($i=1; $i<$len; $i++) {  foreach(array_chunk($ar, $len/2) as $v) {    echo join('', $v), PHP_EOL;  }  $t = array_splice($ar, 1, 1);  $ar = array_merge($ar, $t);  echo PHP_EOL;}

Copy after login
1234
5678

1345
6782

1456
7823

1567
8234

1678
2345

1782
3456

1823
4567

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

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,

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? Apr 01, 2025 pm 03:09 PM

An official introduction to the non-blocking feature of ReactPHP in-depth interpretation of ReactPHP's non-blocking feature has aroused many developers' questions: "ReactPHPisnon-blockingbydefault...

See all articles