๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“– Problem Solution

๋ฐฑ์ค€์•Œ๊ณ ๋ฆฌ์ฆ˜ 1๋‹จ๊ณ„ 6,7,8,9,10,11 Swift

by Fomagran ๐Ÿ’ป 2020. 1. 14.
728x90
๋ฐ˜์‘ํ˜•

ํ•˜...ํ‹ฐ์Šคํ† ๋ฆฌ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ ๋ ‰๊ฑธ๋ ค์„œ ๋‹ค์‹œ ์“ด๋‹ค.... ๊ธ์ •์ ์œผ๋กœ ๊ฐ€๋ณด์ž..

6.๋‘ ์ •์ˆ˜ A์™€ B๋ฅผ ์ž…๋ ฅ๋ฐ›์€ ๋‹ค์Œ, A-B๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

ํ•ต์‹ฌ ์œ„ ๋ฌธ์ œ์—์„œ ์–ธ๊ธ‰ readLineํ•  ๋•Œ L๋Œ€๋ฌธ์ž์ž„

A์™€ B๋ฅผ ๊ตณ์ด ๋”ฐ๋กœ ์“ธ ํ•„์š”๊ฐ€ ์—†์Œ ์ฃผ์˜ํ•  ์  Int(linearr[n])์— default ๊ฐ’์„ ์ฃผ๋ ค๋ฉด ์ „์ฒด๋ฅผ ๋ฌถ์–ด์•ผ ํ•จ

import Foundation

let line = readLine()
let linearr = line!.components(separatedBy:" ")
let A = Int(linearr[0])!
let B = Int(linearr[1])!
let AB = (Int(linearr[0]) ?? 0 - (Int(linearr[1]) ?? 0)
print(A-B)

 

7. ๋‘ ์ •์ˆ˜ A์™€ B๋ฅผ ์ž…๋ ฅ๋ฐ›์€ ๋‹ค์Œ, A×B๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

ํ•ต์‹ฌ line์„ ๊ตณ์ด ๋งŒ๋“ค ํ•„์š”๊ฐ€ ์—†์Œ 

import Foundation

let lineArr = readLine()?.components(separatedBy: " ") ?? [""]
let A = Int(lineArr[0]) ?? 0
let B = Int(lineArr[1]) ?? 0
print(A*B)

 

8.๋‘ ์ •์ˆ˜ A์™€ B๋ฅผ ์ž…๋ ฅ๋ฐ›์€ ๋‹ค์Œ, A/B๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์‹ค์ œ ์ •๋‹ต๊ณผ ์ถœ๋ ฅ๊ฐ’์˜ ์ ˆ๋Œ€์˜ค์ฐจ ๋˜๋Š” ์ƒ๋Œ€์˜ค์ฐจ๊ฐ€ 10-9 ์ดํ•˜์ด๋ฉด ์ •๋‹ต์ด๋‹ค. - 9์ž๋ฆฌ๊นŒ์ง€ ์ถœ๋ ฅํ•˜๋ผ๋Š” ๊ฒƒ

ํ•ต์‹ฌ 1.์†Œ์ˆ˜์  n์งธ ์ž๋ฆฌ๊นŒ์ง€ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์–ด? - String(format:"%.n",Double or Float)

2.Floatํ•˜๊ณ  Double์ฐจ์ด์  ์•Œ์•„? - Float์€ 8์ž๋ฆฌ์ดํ•˜ ์†Œ์ˆ˜์ ๊นŒ์ง€ Double์€ 16์ž๋ฆฌ์ดํ•˜ ์†Œ์ˆ˜์ ๊นŒ์ง€

import Foundation

let lineArr = readLine()?.components(separatedBy: " ") ?? [""]
let A = Double(lineArr[0]) ?? 0
let B = Double(lineArr[1]) ?? 0
let AB = String(format:"%.9f",A/B)
print(AB)

 

9. ๋‘ ์ž์—ฐ์ˆ˜ A์™€ B๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด๋•Œ, A+B, A-B, A*B, A/B(๋ชซ), A%B(๋‚˜๋จธ์ง€)๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

ํ•ต์‹ฌ ์œ„ ๋ฌธ์ œ์—์„œ ๋‹ค ์–ธ๊ธ‰

import Foundation

let lineArr = readLine()?.components(separatedBy: " ") ?? [""]
let A = Int(lineArr[0]) ?? 0
let B = Int(lineArr[1]) ?? 0
print("\(A+B)\n\(A-B)\n\(A*B)\n\(A/B)\n\(A%B)")

 

10. (A+B)%C๋Š” (A%C + B%C)%C ์™€ ๊ฐ™์„๊นŒ?

(A×B)%C๋Š” (A%C × B%C)%C ์™€ ๊ฐ™์„๊นŒ?

์„ธ ์ˆ˜ A, B, C๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์œ„์˜ ๋„ค ๊ฐ€์ง€ ๊ฐ’์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

ํ•ต์‹ฌ ์œ„ ๋ฌธ์ œ์—์„œ ๋‹ค ์–ธ๊ธ‰

import Foundation

let lineArr = readLine()?.components(separatedBy: " ") ?? [""]
let A = Int(lineArr[0]) ?? 0
let B = Int(lineArr[1]) ?? 0
let C = Int(lineArr[2]) ?? 0
print("\((A+B)%C)\n\((A%C+B%C)%C)\n\((A*B)%C)\n\((A%C*B%C)%C)")

 

11.(์„ธ ์ž๋ฆฌ ์ˆ˜) × (์„ธ ์ž๋ฆฌ ์ˆ˜)๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ณผ์ •์„ ํ†ตํ•˜์—ฌ ์ด๋ฃจ์–ด์ง„๋‹ค.

(1)๊ณผ (2)์œ„์น˜์— ๋“ค์–ด๊ฐˆ ์„ธ ์ž๋ฆฌ ์ž์—ฐ์ˆ˜๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ (3), (4), (5), (6)์œ„์น˜์— ๋“ค์–ด๊ฐˆ ๊ฐ’์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

ํ•ต์‹ฌ ๋„ˆ 1์˜์ž๋ฆฌ 10์˜์ž๋ฆฌ 100์˜์ž๋ฆฌ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ์–ด? - 1์˜์ž๋ฆฌ n%10 10์˜์ž๋ฆฌ (n/10)%10 100์˜ ์ž๋ฆฌ n/100

10์˜์ž๋ฆฌ ๊ตฌํ•˜๊ธฐ๊ฐ€ ์ข€ ๊ณ ๋ฏผ๋๋Š”๋ฐ ๋ฐ”๋กœ ํ•ด๊ฒฐ

import Foundation

let A = Int(readLine() ?? "") ?? 0
let B = Int(readLine() ?? "") ?? 0
print("\(A*(B%10))\n\(A*((B/10)%10))\n\(A*(B/100))\n\(A*B)")
728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€