2011-08-09 20 views
1

我要生成所有可能的顺序为49 6的彩票产生尽可能多的序列49/6彩票

所以我需要从1组得出6球,以49

我能得到数列生成这些序列的逻辑?

+2

家庭作业?大多数体面的编程书籍可以告诉你如何编写一个简单的循环来为你做。问题是你将如何处理这些数字?将它们存储在文本文件中? – ccozad

+1

虽然不是一个确切的重复,但我认为它应该足够:http://stackoverflow.com/questions/127704/algorithm-to-return-all-combinations-of-k-elements-from-n – Yoshi

+0

如果它是与更换它是49^6排列(13,841,287,201)。如果每个球没有被替换,它是49 * 48 * 47 * 46 * 45 * 44排列(10,068,347,520) – ccozad

回答

3

这是一篇写得很好的文章,涵盖了组合和排列的各种场景。它在文章结尾还有一个很好的参考列表。

http://www.codingthewheel.com/archives/exhaustively-enumerating-combinations-and-permutations-in-code

+0

宾果在这里它去...感谢这... – Avinash

+0

文章现在404,和主页只是一个背景图片。这就是为什么只有链接的答案不鼓励。存档版本:https://web.archive.org/web/20130131121518/http://www.codingthewheel.com/archives/exhaustively-enumerating-combinations-and-permutations-in-code – flipchart

1
<?php 

$totalOutcomesEnumerated = 0; 

for ($ball1 = 1; $ball1 < 45; $ball1++) { 
    for ($ball2 = $ball1 + 1; $ball2 < 46; $ball2++) { 
    for ($ball3 = $ball2 + 1; $ball3 < 47; $ball3++) { 
     for ($ball4 = $ball3 + 1; $ball4 < 48; $ball4++) { 
     for ($ball5 = $ball4 + 1; $ball5 < 49; $ball5++) { 
      for ($ball6 = $ball5 + 1; $ball6 < 50; $ball6++) { 
      // Each iteration of this loop visits a single outcome 
      $totalOutcomesEnumerated++; 
      echo $ball1 . ',' . $ball2 . ',' . $ball3 . ',' . $ball4 . ',' . $ball5 . ',' . $ball6 . PHP_EOL; 
      } 
     } 
     } 
    } 
    } 
} 

echo 'Total outcomes : ' . $totalOutcomesEnumerated . PHP_EOL ; // 13983816