0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Pythonで重複ありの組み合わせ

Last updated at Posted at 2020-04-20

ここではPythonを使って重複ありの組み合わせが何通りあるかを計算するコードを書いていきます。

factorialを使う。


from math import factorial

print("2つの正の整数を入力してください。")
a, b= map(int, input().split())
h = (factorial(a+b-1))/(factorial(b)*factorial(a-1))
print(str(a)+"個のものから"+str(b)+"個のものを重複を許して選ぶ方法は"+str(h)+"通りです。")

以下の様に書くとmathに含まれる関数を全部見れます。

import math
help(math)

factorialを使わない。

print("2つの正の整数を入力してください。")
a, b= map(int, input().split())
n = a + b -1
m = n
l = a-1
while  n > b+1:
    m = m * (n-1)
    n = n - 1
while l > 0:
    m = m / l
    l = l -1

print(str(a)+"個のものから"+str(b)+"個のものを重複を許して選ぶ方法は"+str(m)+"通りです。")
0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?