python小工具:计算多少种组合方式

  • A+
所属分类:Python习题

代码要求:

红绿蓝三个球,红有3个球,绿有3个球,蓝有6个球,每次搞出有8个球,共有多少个组合。

代码块:

  1. for i in range(0,4):
  2.     for a in range(0,4):
  3.         for b in range(2,7):
  4.             if i +a +b ==8:
  5.                 print(i,a,b)

for b in range(2,7)有个问题,就是每次有8个球,红绿全部加起来的数量只有6个,所以蓝起步必须是2个球。那为什么是7?呃,你难道不知道不取最后一个值吗?(2,7)产出的数字为(2,3,4,5,6)

组合结果

  1. 0 2 6
  2. 1 1 6
  3. 1 2 5
  4. 2 0 6
  5. 2 1 5
  6. 2 2 4

 

李金龙

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: