【问题标题】:how to get all possible combinations of digits until 789?如何获得直到 789 的所有可能的数字组合?
【发布时间】:2016-08-09 17:59:33
【问题描述】:

我正在尝试编写算法来获取所有组合并同时增加一并得到这个

function generate($index){
    $xxx = '';
    $flag = 0;  
    $lengtchString = strlen($index); 
    for($y = 0; $y != $lengtchString; $y++) {
        $xxx .= "$y";
    }
    while ($flag != $lengtchString ) 
    { 
        for ($i = 0; $i<$lengtchString-1; $i++) 
        {            
            $temp = $xxx[$i];  
            $xxx[$i] = $xxx[$i+1]; 
            $xxx[$i+1] = $temp;   
            echo $xxx."<br>"; 
        } 
        $flag++; 
    }
}
generate('abc');

输出是

102
120
210
201
021
012

我需要获取所有组合,不仅是3 数字,还包括所有组合。

例如,如果我写 'abc'... 我需要像

这样的输出
102
120
210
201
021
012
256
874
569
236
254
028

等等....直到789 在数字不会重复的情况下... 我的想法实际上被吹了,无法为此获得适当的算法。提前致谢

【问题讨论】:

  • 你已经使用 $y != $lengtchString 来获取 $xxx 数据,所以它永远不会像 256 那样给你输出,因为长度总是 3,它会在你的输出中给你 0,1,2 的组合.
  • 我知道,但我不介意获取正确的代码,您还有其他建议吗?
  • 你的最后一个例子有点误导我假设你想要所有 3 位数字,其中数字来自 { 0,1,2,3,4,5,6,7,8,9, } 并且每个数字在一个数字中是不同的(所以 111 无效)在那里任何其他限制,或者您只是错过了几个起始组合...请参阅iterative permutations without repetitions in C++,如果该数字已被使用,您只需添加到您的程序flag...

标签: php string algorithm combinations digit


【解决方案1】:

查看此链接PHP algorithm to generate all combinations of a specific size from a single set

    <?php
    function sampling($chars, $size, $combinations = array()) {
    $charsArray = str_split($chars);
    # if it's the first iteration, the first set 
    # of combinations is the same as the set of characters
    if (empty($combinations)) {
        $combinations = $charsArray;
    }

    # we're done if we're at size 1
    if ($size == 1) {
        return $combinations;
    }

    # initialise array to put new values in
    $new_combinations = array();

    # loop through existing combinations and character set to create strings
    foreach ($combinations as $combination) {
        foreach ($charsArray as $char) {
            $new_combinations[] = $combination . $char;
        }
    }
    # call same function again for the next iteration
    return sampling($chars, $size - 1, $new_combinations);

}

// example

$output = sampling("0123456789", 3);
print "<pre>";print_r($output);

【讨论】:

  • 人,感谢您的回复...但是如果我要写 012,这直到 9 点才会打印出来,我尝试了这个算法,但没有输出我需要的东西
  • 此示例适用于您在结果中需要的任何组合,例如,如果您传递“0,1,2”,您将获得“0”、“1”和“2”@HrachoAvagyan 的所有组合跨度>
  • 非常感谢......还有一个小问题,如果函数参数只有一个并且它是一个字符串,如何使用它?即“012”。不是数组。
  • @HrachoAvagyan 我已经更新了代码,请检查现在我们必须传递字符串,例如采样(“012”,3); ,请让我知道这段代码对你有用吗?
  • 效果很好,但我想尝试这种非递归方法。尝试仅使用明确的迭代进行组合。
【解决方案2】:

Delphi 函数从给定字符集中生成所有字符组合,没有字符重复(如果源字符串中没有重复)。

它是如何工作的:
让我们在某个阶段有

charset = '1203456789'
incomplete result = '12'
i goes from 3 to 10

i = 3:  charset doesn't change; recursive call executes with '123' and StartIndex = 4
i = 4: charset changes to '1204356789'; recursive call executes with '124' and StartIndex = 4; 
       charset changes back to '1203456789'
i = 5: charset changes to '12054356789'; recursive call executes with '125' and StartIndex = 4; 
       charset changes back to '1203456789'

等等……

procedure GenString(StartIndx, MaxLen: Integer; Charset, Result: string);

procedure Swap(a,b: Integer);
var
  t: Char;
begin
  t := CharSet[a];
  CharSet[a] := CharSet[b];
  CharSet[b] := t;
end;

var
  i: Integer;
begin
  if Length(Result) = MaxLen then
    Memo1.Lines.Add(Result)  // output result
  else begin
    for i := StartIndx to Length(Charset) do begin
      Swap(i, StartIndx);  //save current char to avoid further usage
      GenString(StartIndx + 1, MaxLen, Charset, Result + Charset[StartIndx]);
      Swap(i, StartIndx); //restore order
    end;
  end;
end;

usage: //Delphi strings are 1-based!
  GenString(1, 3, '0123456789', '');

输出(720 个值):

012
013
014
015
016
017
018
019
021
023
024
025
026
027
028
029
032
031
...
986
987
981
980
902
903
904
905
906
907
908
901

【讨论】:

  • 感谢您的回复,这是用哪种编程语言编写的? C++?这个我看不懂
  • 谢谢,我会试着把这个转换成php
  • 你能帮我一点忙吗?如何将 Memo1.Lines.Add(Result) 转换为 php 。以及程序交换到 php。谢谢。
  • Memo1.Lines.Add(Result) 只是将结果字符串输出到屏幕。可能是echo? Swap 在给定索引处交换字符串中的两个符号 - 就像您在代码中交换 $xxx[$i] 和 $xxx[$i+1]
  • 是的,它的回声,非常感谢你:) ...所以你正在使用我理解的递归
猜你喜欢
  • 1970-01-01
  • 2019-10-25
  • 2013-02-12
  • 1970-01-01
  • 2012-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-05
相关资源
最近更新 更多