duancai7568 2010-08-06 17:00
浏览 86
已采纳

如何计算最大值 可能的组合数量? [重复]

Possible Duplicates:
Display possible combinations of string
algorithm that will take numbers or words and find all possible combinations

If I have 3 strings, such as:

"abc def xyz"

And I want to find the max number of combinations I can generate by rearranging these strings, e.g:

  • abc xyz def
  • def xyz abc
  • xyz abc def

etc. What's the formula/algorithm for calculating this?

  • 写回答

4条回答 默认 最新

  • dqsk4643 2010-08-06 17:06
    关注

    This is not a combination but permutation. The algorithm is n! where n is the number of elements.

    Why ?

    Because you have 3 values to place on three places, so for the first place You have three option, for second only two (becuase You already palace first string) and at the end You have only one option.

    3 * 2 * 1 = 3! = 6

    But if You can repeate those chooses than you have permutation with repetition

    So for first place You can chose from 3 string and for the second also and so one

    3 * 3 * 3 = 3^3 = 27

    n^k - where n is the number of strings and k - the number of "locations"

    And the code algorithm look like this:

    function fact($n)
    {
      if ($n == 0)
      {
        return 1;
      }
      else
      {
        return $n * fact($n - 1);
      }
    }
    

    This is a recursive example

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 用三极管设计—个共射极放大电路
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示