PHPの単一のセットから特定のサイズのすべての組み合わせを生成します
単一のセットから特定のサイズのすべての組み合わせを生成するためのコードは次のとおりです-
例
function sampling($chars, $size, $combinations = array()) {
# in case of first iteration, the first set of combinations is the same as the set of characters
if (empty($combinations)) {
$combinations = $chars;
}
# size 1 indicates we are done
if ($size == 1) {
return $combinations;
}
# initialise array to put new values into it
$new_combinations = array();
# loop through the existing combinations and character set to create strings
foreach ($combinations as $combination) {
foreach ($chars as $char) {
$new_combinations[] = $combination . $char;
}
}
# call the same function again for the next iteration as well
return sampling($chars, $size - 1, $new_combinations);
}
$chars = array('a', 'b', 'c');
$output = sampling($chars, 2);
var_dump($output); 出力
これにより、次の出力が生成されます-
array(9) { [0]=> string(2) "aa" [1]=> string(2) "ab" [2]=> string(2) "ac" [3]=> string(2) "ba" [4]=> string(2) "bb" [5]=> string(2) "bc" [6]=> string(2) "ca" [7]=> string(2) "cb" [8]=> string(2) "cc" } 最初の反復は、表示される同じ文字セットを示します。サイズが1の場合、組み合わせが表示されます。配列は「new_combinations」として初期化され、「forloop」を使用してループされ、その文字列内のすべての文字が他のすべての文字と連結されます。関数「サンプリング」は、パラメータ(文字列、文字列のサイズ、および配列)を使用して呼び出されます。
-
C++の文字列からすべての整数を抽出します
ここでは、C++で文字列からすべての整数を抽出する方法を説明します。数字と数字がない文字列を入れることができます。そこからすべての数値を抽出します。 この問題を解決するために、C++でstringstreamクラスを使用します。文字列を単語ごとに切り取り、整数型のデータに変換してみます。変換が完了すると、整数になり、値が出力されます。 Input: A string with some numbers “Hello 112 World 35 75” Output: 112 35 75 アルゴリズム Step 1:Take a number string Step 2:
-
PHPでimagesetpixel()関数を使用して単一のピクセルを設定するにはどうすればよいですか?
imagesetpixel() は、リストされた座標に単一のピクセルを設定するために使用されるPHPの組み込み関数です。 構文 bool imagesetpixel(resource $image, int $x, int $y, int $color) パラメータ imagesetpixel() 次の4つのパラメータを受け入れます: $ image 、 $ x 、 $ y および$color 。 $ image −作業する画像リソースを指定します。 $ x −ピクセルのx座標を指定します。 $ y −ピクセルのy座標を指定します。 $ col