๋ฐฑ์ค€ - ATM(Swift)

2022. 10. 22. 21:56ใ†Algorithm

๋ฐฑ์ค€ - ATM(Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

https://www.acmicpc.net/problem/11399

 

11399๋ฒˆ: ATM

์ฒซ์งธ ์ค„์— ์‚ฌ๋žŒ์˜ ์ˆ˜ N(1 ≤ N ≤ 1,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ๊ฐ ์‚ฌ๋žŒ์ด ๋ˆ์„ ์ธ์ถœํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„ Pi๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ Pi ≤ 1,000)

www.acmicpc.net

 

 

 ๋‚˜์˜ ํ’€์ด

 

import Foundation

let n = readLine()!
var input = readLine()!.components(separatedBy:" ")
var arr = input.map{ Int($0)! }.sorted(by: <)
var sum = 0
for i in 0..<arr.count {
    for j in 0...i {
        sum += arr[j]
    }
}
print(sum)

 

 ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด

 

sum๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜ ๋” ๋งŒ๋“ค์–ด์„œ for๋ฌธ์ด ๋Œ๋•Œ๋งˆ๋‹ค ๋ˆ„์ ์‹œํ‚ค๋„๋ก ํ–ˆ๋‹ค.

for๋ฌธ์ด ํ•˜๋‚˜๋งŒ ์“ฐ์—ฌ์„œ ๋” ํšจ์œจ์ ์ด๋‹ค.

 

import Foundation

let n = readLine()!
var input = readLine()!.components(separatedBy:" ")
var arr = input.map{ Int($0)! }.sorted(by: <)
var sum = 0
var sum2 = 0
for i in 0..<arr.count {
    sum += arr[i]
    sum2 += sum
}
print(sum2)