Algorithm/Baekjoon

swift 백준 1330, 9498, 14681, 2420, 2753 (조건)

ytw_developer 2024. 7. 13. 15:38

1330 두 수 비교하기

  • readLine() 메서드로 키보드로부터 String을 입력 받습니다.
  • 이후 조건에 맞게 코드 작성합니다
let line = readLine()!
let arr = line.split(separator: " ")

let a = Int(arr[0])!
let b = Int(arr[1])!

if a < b {
    print("<")
} else if a > b {
    print(">")
} else {
    print("==")
}

 

9498 시험 성적

  • 간단하게 조건에 맞게 실행합니다
let line = readLine()!
let arr = line.split(separator: " ")

let a = Int(arr[0])!
//let b = Int(arr[1])!

if a <= 100 && 90 <= a {
    print("A")
} else if a >= 80 {
    print("B")
} else if a >= 70 {
    print("C")
} else if a >= 60 {
    print("D")
} else {
    print("F")
}

 

 

14681 사분면 고르기

너무 간단하므로 생략

let A = readLine()
let B = readLine()

let a = Int(A!)!
let b = Int(B!)!

if a < 0 && b < 0 {
    print(3)
} else if a < 0 && b > 0 {
    print(2)
} else if a > 0 && b < 0 {
    print(4)
} else {
    print(1)
}

 

2420 사파리월드

  • abs 를 사용하여 절대값을 구하는 것이 포인트입니다.
let line = readLine()!
let arr = line.split(separator: " ")

let a = Int(arr[0])!
let b = Int(arr[1])!

print(abs(a-b))

 

 

2753 윤년

  • 윤년의 조건은 다음과 같습니다
    - 4의 배수이면서 (필수 조건)
    - 100의 배수가 아니거나 또는 400의 배수인 경우 -> 하지만 이 2가지 조건 중 하나는 반드시 성립해야 합니다.
let line = readLine()!
let a = Int(line)!

if (a%4==0) && ((!(a%100==0)) || (a%400==0)) {
    print(1)
} else {
    print(0)
}