[1階]模擬試験


これはプログラマーの問題です.
https://programmers.co.kr/learn/courses/30/lessons/42840

ポスト


問題の所在を理解し、与えられた条件をよく観察する.
import Foundation

func solution(_ answers:[Int]) -> [Int] {
    let a: [Int] = [1, 2, 3, 4, 5]
    let b: [Int] = [2, 1, 2, 3, 2, 4, 2, 5]
    let c: [Int] = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]
    var aCount = 0
    var bCount = 0
    var cCount = 0
    var winner: [Int] = []
    
    for i in 1...answers.count {
        
        if i % a.count == 0 { // i가 5의배수일떄
            if answers[i-1] == a[a.count - 1] {
                aCount+=1
            }
        } else {
            if answers[i-1] == a[i % a.count - 1] {
                aCount+=1
            }
        }
        //b
        if i % b.count == 0 {
            if answers[i-1] == b[b.count - 1] {
                bCount+=1
            }
        } else {
            if answers[i-1] == b[i % b.count - 1]  {
                bCount+=1
            }
        }
        //c
        if i % c.count == 0 {
            if answers[i-1] == c[c.count - 1] {
                cCount+=1
            }
        } else {
            if answers[i-1] == c[i % c.count - 1]  {
                cCount+=1
            }
        }
    }
    
    if aCount >= bCount && aCount >= cCount {
        winner.append(1)
    }
    if bCount >= aCount && bCount >= cCount {
        winner.append(2)
    }
    if cCount >= aCount && cCount >= bCount {
        winner.append(3)
    }

    return winner
}