0
0

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 1 year has passed since last update.

【競プロ/Swift】AtCoder Beginner Contest 296

Posted at

注意事項

本記事は復習のための日記的なものであり、丁寧な解説を求めている方には不十分かと思われますので、ご了承のほどお願いいたします。

正解済み問題

A - Alternately

問題文
こちら問題ページ

解答
一つ前の値と比較し続け、値が交互に配置されているかを判定する。

let n = Int(readLine()!)!
let s = Array(readLine()!)

var isCorrect = true
var preS = s[0]
for index in 1..<n {
    if preS == s[index] {
        isCorrect = false
        break
    }
    preS = s[index]
}

print(isCorrect ? "Yes" : "No")

B - Chessboard

問題文
こちら問題ページ

解答
座標の指標となる配列を用意し、あとはループ処理を回して*が回ってきた時の座標を出力する。

let positonMark = [["a","b","c","d","e","f","g","h"],["8","7","6","5","4","3","2","1"]]
var field = [[String]]()

for _ in 0..<8 {
    field.append(Array(readLine()!).map{String($0)})
}

var position = ""

for row in 0..<field.count {
    for column in 0..<field[row].count {
        if field[row][column] == "*" {
            position = positonMark[0][column] + positonMark[1][row]
            break
        }
    }
}

print(position)

誤答及び、未解答問題

C - Gap Existence

問題文
こちら問題ページ

未解答
解き方がわからなかったため、未解答。

正答
順番に目標との差分を配列に加えていき、その配列の中に該当の値があれば X=Ai - Aii のパターンがあるということになる。

let nx = readLine()!.split(separator: " ").map{Int($0)!}
let a = readLine()!.split(separator: " ").map{Int($0)!}
var values: Set<Int> = []
var ans = false
for aij in a {
  let comp1 = nx[1] + aij
  let comp2 = -(nx[1] - aij)
  values.insert(aij)
  if values.contains(comp1) || values.contains(comp2) {
    ans = true
    break
  }
}
print(ans ? "Yes" : "No")

参考

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?