ホームページ バックエンド開発 PHPチュートリアル PHP を使用して、7 つの数値 (1、2、3、4、5、6、7) の配列から 5 つの数値の一意の組み合わせをすべて生成するにはどうすればよいですか?

PHP を使用して、7 つの数値 (1、2、3、4、5、6、7) の配列から 5 つの数値の一意の組み合わせをすべて生成するにはどうすればよいですか?

Dec 04, 2024 am 05:53 AM

How can I generate all unique combinations of 5 numbers from an array of 7 numbers (1, 2, 3, 4, 5, 6, 7) using PHP?

PHP 配列の組み合わせ

7 つの数値 (1、2、3、4、5、6、7) の配列が与えられます。 。目標は、その配列から 5 つの数字の可能な組み合わせをすべて見つけることです。各組み合わせは一意でなければなりません。つまり、重複は許可されません。たとえば、(1,2,3,4,5) と (5,4,3,2,1) は同じ組み合わせとみなされます。

Solution

考えられる解決策の 1 つは、Combinations クラスを使用することです。これは、Iterator インターフェイスを実装し、指定された数値のすべての可能な組み合わせを反復処理する方法を提供します。その仕組みは次のとおりです:

class Combinations implements Iterator
{
    protected $c = null; // Combination of numbers
    protected $s = null; // Source array
    protected $n = 0; // Number of elements in the array
    protected $k = 0; // Number of elements in each combination
    protected $pos = 0; // Current position of the iterator

    function __construct($s, $k) {
        // Initialize the class properties
        if(is_array($s)) {
            $this->s = array_values($s);
            $this->n = count($this->s);
        } else {
            $this->s = (string) $s;
            $this->n = strlen($this->s);
        }
        $this->k = $k;
        $this->rewind();
    }

    // Return the current key
    function key() {
        return $this->pos;
    }

    // Return the current value
    function current() {
        $r = array();
        for($i = 0; $i < $this->k; $i++)
            $r[] = $this->s[$this->c[$i]];
        return is_array($this->s) ? $r : implode('', $r);
    }

    // Move to the next combination
    function next() {
        if($this->_next())
            $this->pos++;
        else
            $this->pos = -1;
    }

    // Rewind to the first combination
    function rewind() {
        $this->c = range(0, $this->k);
        $this->pos = 0;
    }

    // Check if the iterator is valid (at a valid position)
    function valid() {
        return $this->pos >= 0;
    }

    // Move to the next combination (internal function)
    protected function _next() {
        $i = $this->k - 1;
        while ($i >= 0 && $this->c[$i] == $this->n - $this->k + $i)
            $i--;
        if($i < 0)
            return false;
        $this->c[$i]++;
        while($i++ < $this->k - 1)
            $this->c[$i] = $this->c[$i - 1] + 1;
        return true;
    }
}

// Create a Combinations object for the given array and number of elements per combination
$combinations = new Combinations("1234567", 5);

// Iterate over all possible combinations and print them out
foreach($combinations as $substring)
    echo $substring, ' ';
ログイン後にコピー

このコードは次の出力を生成します:

12345 12346 12347 12356 12357 12367 12456 12457 12467 12567 13456 13457 13467 13567 14567 23456 23457 23467 23567 24567 34567 
ログイン後にコピー

以上がPHP を使用して、7 つの数値 (1、2、3、4、5、6、7) の配列から 5 つの数値の一意の組み合わせをすべて生成するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットな記事タグ

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

11ベストPHP URLショートナースクリプト(無料およびプレミアム) 11ベストPHP URLショートナースクリプト(無料およびプレミアム) Mar 03, 2025 am 10:49 AM

11ベストPHP URLショートナースクリプト(無料およびプレミアム)

Laravelでフラッシュセッションデータを使用します Laravelでフラッシュセッションデータを使用します Mar 12, 2025 pm 05:08 PM

Laravelでフラッシュセッションデータを使用します

Laravelテストでの簡略化されたHTTP応答のモッキング Laravelテストでの簡略化されたHTTP応答のモッキング Mar 12, 2025 pm 05:09 PM

Laravelテストでの簡略化されたHTTP応答のモッキング

Instagram APIの紹介 Instagram APIの紹介 Mar 02, 2025 am 09:32 AM

Instagram APIの紹介

LaravelのバックエンドでReactアプリを構築する:パート2、React LaravelのバックエンドでReactアプリを構築する:パート2、React Mar 04, 2025 am 09:33 AM

LaravelのバックエンドでReactアプリを構築する:パート2、React

PHPのカール:REST APIでPHPカール拡張機能を使用する方法 PHPのカール:REST APIでPHPカール拡張機能を使用する方法 Mar 14, 2025 am 11:42 AM

PHPのカール:REST APIでPHPカール拡張機能を使用する方法

Codecanyonで12の最高のPHPチャットスクリプト Codecanyonで12の最高のPHPチャットスクリプト Mar 13, 2025 pm 12:08 PM

Codecanyonで12の最高のPHPチャットスクリプト

2025 PHP状況調査の発表 2025 PHP状況調査の発表 Mar 03, 2025 pm 04:20 PM

2025 PHP状況調査の発表

See all articles