Bibi's DevLog ๐Ÿค“๐ŸŽ

[๋ฐฑ์ค€ 9094] ์ˆ˜ํ•™์  ํ˜ธ๊ธฐ์‹ฌ (Swift) ๋ณธ๋ฌธ

ํ”„๋กœ๊ทธ๋ž˜๋ฐ/์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ’€์ด Swift

[๋ฐฑ์ค€ 9094] ์ˆ˜ํ•™์  ํ˜ธ๊ธฐ์‹ฌ (Swift)

๋น„๋น„ bibi 2022. 12. 7. 12:39

9094๋ฒˆ: ์ˆ˜ํ•™์  ํ˜ธ๊ธฐ์‹ฌ

  • ๋ฒ”์œ„๊ฐ€ ํฌ์ง€ ์•Š๊ณ (0โ€ฆ100) ์ •์ˆ˜ํ˜•์ด๋ฏ€๋กœ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ–ˆ๋‹ค.
  • Int์ธ Double ํŒ๋ณ„, ์ฆ‰ Double์˜ ์†Œ์ˆ˜์  ์ดํ•˜๊ฐ€ 0์ธ์ง€ ์•„๋‹Œ์ง€ ํŒ๋ณ„ํ•˜๊ธฐ๋Š” ์—ฌ๊ธฐ์— ๋”ฐ๋กœ ์ •๋ฆฌํ•ด ๋‘์—ˆ๋‹ค.

ํ’€์ด

(69104KB, 108ms)

let count = Int(readLine()!)!
(1...count).forEach { _ in
    let input = readLine()!.split(separator: " ").map { Int($0)! }
    let n = input[0]
    let m = input[1]
    var pairs = 0
    (1..<n).forEach { a in
        (a+1..<n).forEach { b in // a < b
            let value = Double(((a*a) + (b*b) + m)) / Double(a*b) // Int๋ผ๋ฆฌ ๋‚˜๋ˆ„๋ฉด ์†Œ์ˆ˜์ ์„ ๋ฒ„๋ฆผ
            if value == Double(Int(value)) { // Double์ด Int์ธ์ง€ ์•„๋‹Œ์ง€ ํŒ๋ณ„
                pairs += 1
            }
        }
    }
    print(pairs)
}

ํ’€์ด2

๋‹ค๋ฅธ ํ’€์ด๋“ค์„ ๋ณด๊ณ  (a์ œ๊ณฑ) + (b์ œ๊ณฑ) + m ์„ (a*b) ๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€ (% ์‚ฌ์šฉ)๊ฐ€ 0์ด๋ฉด ์†Œ์ˆ˜์  ์ดํ•˜๊ฐ€ 0์ž„์„ ํŒ๋ณ„ํ•  ์ˆ˜ ์žˆ์–ด์„œ ๊ทธ๋ ‡๊ฒŒ๋„ ํ•ด๋ดค๋Š”๋ฐ, ์ด์ชฝ์ด ์‹œ๊ฐ„์ด ์กฐ๊ธˆ ๋” ๊ฑธ๋ ธ๋‹ค.

(69104KB, 128ms)

let count = Int(readLine()!)!
(1...count).forEach { _ in
    let input = readLine()!.split(separator: " ").map { Int($0)! }
    let n = input[0]
    let m = input[1]
    var pairs = 0
    (1..<n).forEach { a in
        (a+1..<n).forEach { b in
            let value = (a*a) + (b*b) + m
            if value % (a*b) == 0 {
                pairs += 1
            }
        }
    }
    print(pairs)
}