如何找到具有2个属性的3个对象的所有可能组合,这些属性将总计为3个不同的属性

时间:2014-08-27 17:33:00

标签: math combinations formula

我试图找出代表以下情况的数学公式:

  1. 我有X种颜色,Y球,每种都有2种颜色。
  2. 我通过以下方式计算Y球的数量: Y = X! /(X - 2)! * 2!
  3. 现在,我的任务是找到3个球组合的总量,将采用以下形式:

    1. 第一球和第二球有一种相似的颜色。
    2. 第一球和第二球的第二种颜色不得相同。
    3. 第三个球具有另外两个球的颜色(它们之间不共享)
    4. 我编写了一个代码,可以帮助我模拟我正在寻找的内容,但是无法将其反向设计为可以帮助我计算数值结果而无需模拟它的公式。

      代码:

      var Colors = ['red', 'blue', 'green', 'yellow', 'white' , 'black'];
      
      C = Colors.length;
      document.write("Balls: " + Colors.length + '<br>');
      document.write(Colors.join() + '<br>');
      
      BallsCount = (sFact(C) / (sFact(C-2) * 2));
      document.write("BallsCount: " + BallsCount + '<br>');
      
      var Balls = new Array();
      for (i=0, c=1; i<Colors.length;i++)
        for (x=i+1; x<Colors.length; c++, x++)
        {
          document.write(c + ": " + Colors[i] + '/' + Colors[x] + '<br>');
          Balls[c-1] = Colors[i] + '/' + Colors[x];
        }
      
      //Triangles = BallsCount / 3;
      TrianglesCount = (sFact(BallsCount) / (sFact(BallsCount-3) * sFact(3)));
      document.write("Triangles: " + TrianglesCount + '<br>');
      
      var Triangles = new Array();
      for (i=0, c=1; i<Balls.length;i++)
        for (y=i+1; y<Balls.length; y++)
          for (x=y+1; x<Balls.length; x++)
          {
            if (Balls[i].split('/')[0] == Balls[y].split('/')[0] && ( Balls[i].split('/')[1] + '/' + Balls[y].split('/')[1] == Balls[x] || Balls[y].split('/')[1] + '/' + Balls[i].split('/')[1] == Balls[x]))
            {
              document.write(c + ": " + Balls[i] + " - " + Balls[y] + " - " + Balls[x] + '<br>');
              Triangles[c] = Balls[i] + " - " + Balls[y] + " - " + Balls[x];
              c++;
            }
          }
      
      function sFact(num)
      {
          var rval=1;
          for (var i = 2; i <= num; i++)
              rval = rval * i;
          return rval;
      }
      

      结果:

      Colors: 6
      
      red,blue,green,yellow,white,black
      
      Balls: 15
      
      1. red/blue
      2. red/green
      3. red/yellow
      4. red/white
      5. red/black
      6. blue/green
      7. blue/yellow
      8. blue/white
      9. blue/black
      10. green/yellow
      11. green/white
      12. green/black
      13. yellow/white
      14. yellow/black
      15. white/black
      
      Triangles:
      
      1. red/blue - red/green - blue/green
      2. red/blue - red/yellow - blue/yellow
      3. red/blue - red/white - blue/white
      4. red/blue - red/black - blue/black
      5. red/green - red/yellow - green/yellow
      6. red/green - red/white - green/white
      7. red/green - red/black - green/black
      8. red/yellow - red/white - yellow/white
      9. red/yellow - red/black - yellow/black
      10. red/white - red/black - white/black
      11. blue/green - blue/yellow - green/yellow
      12. blue/green - blue/white - green/white
      13. blue/green - blue/black - green/black
      14. blue/yellow - blue/white - yellow/white
      15. blue/yellow - blue/black - yellow/black
      16. blue/white - blue/black - white/black
      17. green/yellow - green/white - yellow/white
      18. green/yellow - green/black - yellow/black
      19. green/white - green/black - white/black
      20. yellow/white - yellow/black - white/black
      

      计算组合总量的公式是什么?

      另一个问题是,例如,如果爱丽丝有如上所述的一组球,并且鲍勃有一组类似的球,其中一些颜色是爱丽丝所拥有的,而另一些则是她没有的(反之亦然),所以对于N个参与者,我怎样才能计算出来自所有参与者的最大组合3? (每位参赛者超过1球)

      感谢。

1 个答案:

答案 0 :(得分:1)

正如叶戈尔所说:X!/ [(X-r)!(r!)] 订单不重要,不允许重复。 “三角形”的情况是r = 3,6!/(3!3!)= 20