AtCoderに登録したら解くべき精選過去問 10 問を Swift5.2.1 で解いてみた

# 概要
Swiftを勉強する一環で[AtCoder Beginners Selection](https://atcoder.jp/contests/abs)の問題を解いてみたので解答をまとめてみました．詳しい解説は他の方々の書かれた記事を参考にしてください．

# 入出力
入力に使用している```cin >>> a```の```cin```は[この記事](https://qiita.com/OKCH3COOH/items/a639c32f47feab13cd77)の```Input```クラスを使っています．

# 解答
## 例題 PracticeA
```swift
var (a, b, c, s): (Int, Int, Int, String) = (0, 0, 0, "")
cin >>> a >>> b >>> c >>> s

print(a + b + c, s)
```

## 第１問 ABC086A Product
```swift
var (a, b): (Int, Int) = (0, 0)
cin >>> a >>> b

if (a % 2 == 0 || b % 2 == 0) {
    print("Even")
} else {
    print("Odd")
}
```

## 第２問 ABC081A Placing Marbles
```swift
var S: String = ""
cin >>> S

var cnt: Int = 0
for s in S {
    if s == "1" {
        cnt += 1
    }
}

print(cnt)
```

## 第３問 ABC081B Shift only
```swift
var N: Int = 0
cin >>> N

var ans: Int = 10000

for _ in 0..<N {
    var a: Int = 0
    cin >>> a

    var cnt: Int = 0
    while a > 0 && a % 2 == 0 {
        cnt += 1
        a /= 2
    }

    ans = min(ans, cnt)
}

print(ans)
```

## 第４問 ABC087B Coins
```swift
var (A, B, C, X): (Int, Int, Int, Int) = (0, 0, 0, 0)
cin >>> A >>> B >>> C >>> X

var ans: Int = 0
for a in 0...A {
    for c in 0...C {
        var b: Int = X - a * 500 - c * 50

        if b % 100 != 0 {
            continue
        }

        b /= 100

        if 0 <= b && b <= B {
            ans += 1
        }
    }
}

print(ans)
```

## 第５問 ABC083B Some Sums
```swift
var (N, A, B): (Int, Int, Int) = (0, 0, 0)
cin >>> N >>> A >>> B

var ans: Int = 0

func digitSum(_ n: Int) -> Int {
    var (n, ret): (Int, Int) = (n, 0)
    while n > 0 {
        ret += n % 10
        n /= 10
    }
    return ret
}

for i in 1...N {
    let d: Int = digitSum(i)
    if A <= d && d <= B {
        ans += i
    }
}

print(ans)
```

## 第６問 ABC088B Card Game for Two
```swift
var N: Int = 0
cin >>> N

var A = [Int](repeating: 0, count: N)

for i in 0..<N {
    cin >>> A[i]
}

A.sort{$0 > $1}

var ans: Int = 0
for (i, a) in A.enumerated() {
    if i % 2 == 0 {
        ans += a
    } else {
        ans -= a
    }
}

print(ans)
```

## 第７問 ABC085B Kagami Mochi
```swift
var N: Int = 0
cin >>> N

var D = [Int](repeating: 0, count: N)
for i in 0..<N {
    cin >>> D[i]
}

D.sort{$0 < $1}

var ans: Int = 0
var now: Int = 0
for d in D {
    if d > now {
        now = d
        ans += 1
    }
}

print(ans)
```

## 第８問 ABC085C Otoshidama
```swift
func sol() -> () {
    var (N, Y): (Int, Int) = (0, 0)
    cin >>> N >>> Y

    for man in 0...(Y / 10000) {
        for gosen in 0...(Y / 5000) {
            let sen: Int = (Y - man * 10000 - gosen * 5000) / 1000

            if sen < 0 {
                break
            }

            if sen + gosen + man == N {
                print(man, gosen, sen)
                return
            }
        }
    }
    print("-1 -1 -1")
    return
}

sol()
```

## 第９問 ABC049C Daydream
```swift
var S: String = ""
cin >>> S
S = String(S.reversed())

let words: [String] = ["dream", "dreamer", "erase", "eraser"]
var revWords: [String] = []

for w in words {
    revWords.append(String(w.reversed()))
}

var t: String = ""
for s in S {
    t.append(s)
    if revWords.contains(t) {
        t = ""
    }
}
print(t.count == 0 ? "YES" : "NO")
```

## 第10問 ABC086C Traveling
```swift

func isOk() -> Bool {
    var N : Int = 0
    cin >>> N

    var T = [Int](repeating: 0, count: N)
    var X = [Int](repeating: 0, count: N)
    var Y = [Int](repeating: 0, count: N)

    for i in 0..<N {
        cin >>> T[i] >>> X[i] >>> Y[i]
    }

    var (time, x, y) : (Int, Int, Int) = (0, 0, 0)
    for i in 0..<N {
        let dt : Int = T[i] - time
        let dx : Int = X[i] - x
        let dy : Int = Y[i] - y

        let dist : Int = abs(dx) + abs(dy)
        if dt < dist {
            return false
        }

        if dist % 2 != dt % 2 {
            return false
        }

        time = T[i]
        x = X[i]
        y = Y[i]
    }
    return true
}

print(isOk() ? "Yes" : "No")
```

# あとがき
文字列操作むずかしいです．