LoginSignup
2

More than 3 years have passed since last update.

Swift:なんか組み合わせ全パターン出すやつ

Posted at
func output(num: Int) {
    for n in (0 ..< Int(pow(2.0, Double(num)))) {
        var d = [Int]()
        for m in (0 ..< num) {
            if n / Int(pow(2.0, Double(m))) % 2 == 0 {
                d.append(0)
            } else {
                d.append(1)
            }
        }
        Swift.print(d)
    }
}
num=4の時の出力結果
[0, 0, 0, 0]
[1, 0, 0, 0]
[0, 1, 0, 0]
[1, 1, 0, 0]
[0, 0, 1, 0]
[1, 0, 1, 0]
[0, 1, 1, 0]
[1, 1, 1, 0]
[0, 0, 0, 1]
[1, 0, 0, 1]
[0, 1, 0, 1]
[1, 1, 0, 1]
[0, 0, 1, 1]
[1, 0, 1, 1]
[0, 1, 1, 1]
[1, 1, 1, 1]

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
2