LoginSignup
0
2

More than 3 years have passed since last update.

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

Posted at

概要

Swiftを勉強する一環でAtCoder Beginners Selectionの問題を解いてみたので解答をまとめてみました.詳しい解説は他の方々の書かれた記事を参考にしてください.

入出力

入力に使用しているcin >>> acinこの記事Inputクラスを使っています.

解答

例題 PracticeA

var (a, b, c, s): (Int, Int, Int, String) = (0, 0, 0, "")
cin >>> a >>> b >>> c >>> s

print(a + b + c, s)

第1問 ABC086A Product

var (a, b): (Int, Int) = (0, 0)
cin >>> a >>> b

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

第2問 ABC081A Placing Marbles

var S: String = ""
cin >>> S

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

print(cnt)

第3問 ABC081B Shift only

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)

第4問 ABC087B Coins

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)

第5問 ABC083B Some Sums

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)

第6問 ABC088B Card Game for Two

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)

第7問 ABC085B Kagami Mochi

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)

第8問 ABC085C Otoshidama

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()

第9問 ABC049C Daydream

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


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")

あとがき

文字列操作むずかしいです.

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