๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
728x90
๋ฐ˜์‘ํ˜•

๐Ÿ“– Problem Solution204

Swift ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์ˆ˜ํฌ์ž ์ˆ˜ํฌ์ž๋Š” ์ˆ˜ํ•™์„ ํฌ๊ธฐํ•œ ์‚ฌ๋žŒ์˜ ์ค€๋ง์ž…๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž ์‚ผ์ธ๋ฐฉ์€ ๋ชจ์˜๊ณ ์‚ฌ์— ์ˆ˜ํ•™ ๋ฌธ์ œ๋ฅผ ์ „๋ถ€ ์ฐ์œผ๋ ค ํ•ฉ๋‹ˆ๋‹ค. ์ˆ˜ํฌ์ž๋Š” 1๋ฒˆ ๋ฌธ์ œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ๊นŒ์ง€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฐ์Šต๋‹ˆ๋‹ค. 1๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ... 2๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 2, 1, 2, 3, 2, 4, 2, 5, 2, 1, 2, 3, 2, 4, 2, 5, ... 3๋ฒˆ ์ˆ˜ํฌ์ž๊ฐ€ ์ฐ๋Š” ๋ฐฉ์‹: 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, 3, 3, 1, 1, 2, 2, 4, 4, 5, 5, ... 1๋ฒˆ ๋ฌธ์ œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋ฌธ์ œ๊นŒ์ง€์˜ ์ •๋‹ต์ด ์ˆœ์„œ๋Œ€๋กœ ๋“ค์€ ๋ฐฐ์—ด answers๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ฐ€์žฅ ๋งŽ์€ ๋ฌธ์ œ๋ฅผ ๋งžํžŒ ์‚ฌ๋žŒ์ด ๋ˆ„๊ตฌ์ธ์ง€ ๋ฐฐ์—ด์— ๋‹ด์•„ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”... 2020. 2. 6.
[Swift] ๋ฐฑ์ค€ while๋ฌธ 1,2,3๋ฒˆ 1.์ž…๋ ฅ์€ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ํ•œ ์ค„๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ๊ฐ ์ค„์— A์™€ B๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (0 < A, B < 10) ์ž…๋ ฅ์˜ ๋งˆ์ง€๋ง‰์—๋Š” 0 ๋‘ ๊ฐœ๊ฐ€ ๋“ค์–ด์˜จ๋‹ค. ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งˆ๋‹ค A+B๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ํ•ต์‹ฌ:x,y๋ฅผ ์ •ํ•ด๋†“๊ณ  while๋ฌธ์— x์™€ y๊ฐ€ 0์ด๋˜๋ฉด while๋ฌธ์ด ๋ฉˆ์ถ”๊ฒŒ ํ•œ๋‹ค. import Foundation var x : Int? var y : Int? while x != 0 && y != 0 { let line = readLine()! let linearr = line.components(separatedBy: " ") let a = Int(linearr[0])! let b = Int(linearr[1])! if a == 0 && b == 0{ x = .. 2020. 2. 1.
๋ฐฑ์ค€ํ”„๋กœ๊ทธ๋ž˜๋ฐ for๋ฌธ 10๋ฒˆ,11๋ฒˆ Swift 10๋ฒˆ. ์ฒซ์งธ ์ค„์—๋Š” ๋ณ„ 1๊ฐœ, ๋‘˜์งธ ์ค„์—๋Š” ๋ณ„ 2๊ฐœ, N๋ฒˆ์งธ ์ค„์—๋Š” ๋ณ„ N๊ฐœ๋ฅผ ์ฐ๋Š” ๋ฌธ์ œ ํ•˜์ง€๋งŒ, ์˜ค๋ฅธ์ชฝ์„ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ ๋ณ„(์˜ˆ์ œ ์ฐธ๊ณ )์„ ์ถœ๋ ฅํ•˜์‹œ์˜ค. ex) 5 * ** *** **** ***** ํ•ต์‹ฌ: stride์•Œ๊ณ ์žˆ๋‹ˆ? terminator์•Œ๊ณ ์žˆ๋‹ˆ? stride๋Š” from๋ถ€ํ„ฐ to๊นŒ์ง€ by๊ฐ’์„ ๋”ํ•ด์ค˜ terminator๋Š” ์•ˆ์— ๋“ค์–ด๊ฐ„ ๊ฐ’์„ ๋”ํ•ด์„œ ์ƒˆ๋กœ์šด ๋ผ์ธ์— ํ•˜์ง€๋ง๊ณ  ๋˜‘๊ฐ™์€ ๋ผ์ธ์— ๋”ํ•ด์ค˜ ์œ„ ๋ฌธ์ œ๋ฅผ ๋ณด๋ฉด ๊ณต๋ฐฑ์ด 4๊ฐœ 3๊ฐœ 2๊ฐœ 1๊ฐœ๋กœ ์ค„์–ด๋“ค์–ด์•ผ ํ•˜๊ณ  ๋ณ„์€ 1๊ฐœ 2๊ฐœ 3๊ฐœ 4๊ฐœ 5๊ฐœ๊นŒ์ง€ ๋Š˜์–ด๋‚˜์•ผํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋‘๊ฐœ์˜ ํฌ๋ฌธ์ด ์กด์žฌํ•ด์•ผํ•˜๋Š”๋ฐ ์šฐ์„  ๊ฐ€์žฅ ๋ฐ”๊นฅ์ชฝ ํฌ๋ฌธ์—” ์ž…๋ ฅ๊ฐ’๋งŒํผ ๋ฐ˜๋ณตํ•˜๋ผ๊ณ  ์‹œํ‚ค๊ณ  ๊ทธ ์•ˆ์—๋‹ค๊ฐ€ ๊ณต๋ฐฑ์„ ์ž…๋ ฅ๊ฐ’๋ถ€ํ„ฐ i๊ฐ’๊นŒ์ง€ ์ฐจ๋ก€๋กœ -1๋งŒํผ ์ค„์–ด๋“ค๋ฉด์„œ ์ƒˆ๋กœ์šด๋ผ์ธ๋ง๊ณ  ๋ถ™์—ฌ์ค˜ ๊ทธ๋Ÿฌ๋ฉด i๊ฐ’์ด 1์ด.. 2020. 1. 29.
๋ฐฑ์ค€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ for๋ฌธ 4,5,6,7๋ฒˆ Swift 4.๋ณธ๊ฒฉ์ ์œผ๋กœ for๋ฌธ ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์ „์— ์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์ด ์žˆ๋‹ค. ์ž…์ถœ๋ ฅ ๋ฐฉ์‹์ด ๋Š๋ฆฌ๋ฉด ์—ฌ๋Ÿฌ ์ค„์„ ์ž…๋ ฅ๋ฐ›๊ฑฐ๋‚˜ ์ถœ๋ ฅํ•  ๋•Œ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ด๋‹ค. C++์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ณ  cin/cout์„ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด, cin.tie(NULL)๊ณผ sync_with_stdio(false)๋ฅผ ๋‘˜ ๋‹ค ์ ์šฉํ•ด ์ฃผ๊ณ , endl ๋Œ€์‹  ๊ฐœํ–‰๋ฌธ์ž(\n)๋ฅผ ์“ฐ์ž. ๋‹จ, ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋” ์ด์ƒ scanf/printf/puts/getchar/putchar ๋“ฑ C์˜ ์ž…์ถœ๋ ฅ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ ๋œ๋‹ค. Java๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๋ฉด, Scanner์™€ System.out.println ๋Œ€์‹  BufferedReader์™€ BufferedWriter๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. BufferedWriter.flush๋Š” ๋งจ ๋งˆ์ง€๋ง‰์— ํ•œ ๋ฒˆ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค. Py.. 2020. 1. 21.
๋ฐฑ์ค€ํ”„๋กœ๊ทธ๋ž˜๋ฐ for ๋ฌธ 1,2,3๋ฒˆ Swift 1.N์„ ์ž…๋ ฅ๋ฐ›์€ ๋’ค, ๊ตฌ๊ตฌ๋‹จ N๋‹จ์„ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ํ•ต์‹ฌ : for ๋ฌธ ์“ธ ์ค„ ์•„๋‹ˆ? import Foundation let number = Int(readLine()!)! for i in 1...9{ print("\(number) * \(i) = \(number*i)") } 2.๋‘ ์ •์ˆ˜ A์™€ B๋ฅผ ์ž…๋ ฅ๋ฐ›์€ ๋‹ค์Œ, A+B๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ์ฒซ์งธ ์ค„์— ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ T๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๊ฐ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋Š” ํ•œ ์ค„๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ๊ฐ ์ค„์— A์™€ B๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (0 < A, B < 10) ํ•ต์‹ฌ: ์šฐ์„  ๋‘ ์ˆ˜๋ฅผ ๋”ํ•œ ๊ฑธ ๋ฐฐ์—ด์•ˆ์— ๋„ฃ๊ณ  ๊ทธ๊ฑธ ์ฐจ๋ก€ ์ฐจ๋ก€ ์ถœ๋ ฅํ•ด๋ด import Foundation let number = Int(readLine()!)! var ab = [Int].. 2020. 1. 20.
Edabit Very Easy Swift Generate a Countdown of Numbers in an Array Example countdown(5) โžž [5, 4, 3, 2, 1, 0] countdown(1) โžž [1, 0] countdown(0) โžž [0] ์ˆซ์ž๋ฅผ ๋„ฃ์œผ๋ฉด ์ˆซ์ž๋ถ€ํ„ฐ 0๊นŒ์ง€ ๋ฐฐ์—ด์—์„œ ๋„ฃ์–ด์„œ ๋ณด์—ฌ์ค˜ ๊ทธ๋Œ€์‹  ์ˆœ์„œ๋Š” ์ˆซ์ž -> 0 ์ˆœ์œผ๋กœ ๋‚ด ํ’€์ด for๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ˆซ์ž์—์„œ 0๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ๋นผ์„œ ๋„ฃ์–ด์ฃผ๋ฉด ๋˜๊ฒ ๋‹ค ์ƒ๊ฐ func countdown(_ start: Int) -> [Int] { var arr = [Int]() for i in 0...start{ arr.append(start-i) } return arr } ๋” ์ข‹์€ ์ •๋‹ต ํ•ต์‹ฌ ๋„ˆ reversed() ์•Œ์•„? func countdown(_ start: Int) -> [In.. 2020. 1. 19.
Edabit Very Easy Swift Find the Largest Number in an Array Example findLargestNum([4, 5, 1, 3]) โžž 5 findLargestNum([300, 200, 600, 150]) โžž 600 findLargestNum([1000, 1001, 857, 1]) โžž 1001 ์ด๋Ÿฐ์‹์œผ๋กœ ๋ฐฐ์—ด์•ˆ์— ๊ฐ€์žฅ ์ˆซ์ž๋ฅผ ์ฐพ๋Š”๊ฑด๋ฐ ๋‚˜๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด for๋ฌธ์„ ์‚ฌ์šฉํ•ด ๊ฐ€์žฅ ํฐ ๊ฑธ ์ฐพ์•˜๋Š”๋ฐ .max()๋ผ๋Š” ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ์—ˆ๋‹ค. ํ•ต์‹ฌ max()๋ฅผ ์•Œ๊ณ ์žˆ๋‹ˆ? func findLargestNum(_ arr: [Int]) -> Int { var a = 0 for i in 0...arr.count-1{ if arr[i] > a{ a = arr[i] } } return a } func findLargestNum(_ ar.. 2020. 1. 18.
Edabit Very Easy Swift 1. String์ด ๋ชจ๋‘ ๋Œ€๋ฌธ์ž๊ฑฐ๋‚˜ ๋ชจ๋‘ ์†Œ๋ฌธ์ž์ธ ๊ฒฝ์šฐ์— true๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ์•„๋‹ˆ๋ฉด false๋ฅผ ์ถœ๋ ฅํ•ด ํ•ต์‹ฌ uppercased()์™€ lowercased()๋ฅผ ์•„๋‹ˆ? - ๋‚ด ์ƒ๊ฐ์—” isuppercased? ์ด๋Ÿฐ ๋ฉ”์†Œ๋“œ๋กœ ํ•ด์•ผํ•˜๋Š” ์ค„ ์•Œ๊ณ  ์ฐพ์•˜๋Š”๋ฐ ์•Œ๊ณ ๋ณด๋‹ˆ ๊ฑ ๋ชจ๋‘ ๋Œ€์†Œ๋ฌธ์ž๋กœ ๋ฐ”๊พผ ๊ฒƒ๊ณผ str์„ ๋น„๊ตํ•ด์„œ ๋˜‘๊ฐ™์œผ๋ฉด true ์•„๋‹ˆ๋ฉด false๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด ๋๋‹ค. func sameCase(_ str: String) -> Bool { return str.uppercased() == str || str.lowercased() == str } 2.1์—์„œ ํ•จ์ˆ˜์— ์ „๋‹ฌํ•œ ์ˆซ์ž๊นŒ์ง€ ๋ชจ๋“  ์ˆซ์ž๋ฅผ ๋”ํ•˜์‹ญ์‹œ์˜ค. ํ•ต์‹ฌ for๋ฌธ ์‚ฌ์šฉํ•  ์ค„ ์•„๋‹ˆ? func addUp(_ num: Int) -> Int { var total = 0 fo.. 2020. 1. 17.
๋ฐฑ์ค€ํ”„๋กœ๊ทธ๋ž˜๋ฐ 2๋‹จ๊ณ„ 1,2,3,4,5,6๋ฒˆ Swift 1. ๋‘ ์ •์ˆ˜ A์™€ B๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, A์™€ B๋ฅผ ๋น„๊ตํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ํ•ต์‹ฌ if๋ฌธ ์“ธ ์ค„์•„๋‹ˆ? import Foundation let line = readLine() let linearr = line!.components(separatedBy:" ") let A = Int(linearr[0])! let B = Int(linearr[1])! if AB{ print(">") }else{ print("==") } 2.์‹œํ—˜ ์ ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ 90 ~ 100์ ์€ A, 80 ~ 89์ ์€ B, 70 ~ 79์ ์€ C, 60 ~ 69์ ์€ D, ๋‚˜๋จธ์ง€ ์ ์ˆ˜๋Š” F๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ํ•ต์‹ฌ 1.else if ์“ธ ์ค„์•„๋‹ˆ? 2.&&์“ธ ์ค„ ์•„๋‹ˆ? import Foundation let line = read.. 2020. 1. 16.
๋ฐฑ์ค€์•Œ๊ณ ๋ฆฌ์ฆ˜ 1๋‹จ๊ณ„ 6,7,8,9,10,11 Swift ํ•˜...ํ‹ฐ์Šคํ† ๋ฆฌ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ ๋ ‰๊ฑธ๋ ค์„œ ๋‹ค์‹œ ์“ด๋‹ค.... ๊ธ์ •์ ์œผ๋กœ ๊ฐ€๋ณด์ž.. 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. ๋‘.. 2020. 1. 14.
Edabit Very easy Multiple of 100 Swift 100์œผ๋กœ ๋‚˜๋ˆ ์ง€๋Š” ํ•จ์ˆ˜ ์ž‘์„ฑํ•˜๊ธฐ //ํ•ต์‹ฌ %๋ฅผ ์•Œ๊ณ ์žˆ๋Š๋ƒ func divisible(_ num: Int) -> Bool { return num%100 == 0 } 2020. 1. 13.
[Swift] ๋ฐฑ์ค€ํ”„๋กœ๊ทธ๋ž˜๋ฐ 1๋‹จ๊ณ„ 1,2,3,4,5๋ฒˆ 1๋ฒˆ Hello World! ์ถœ๋ ฅ print("Hello World") 2๋ฒˆ ๊ฐ•ํ•œ์นœ๊ตฌ ๋Œ€ํ•œ์œก๊ตฐ ๊ฐ•ํ•œ์นœ๊ตฌ ๋Œ€ํ•œ์œก๊ตฐ ์ถœ๋ ฅ ํ•ต์‹ฌ \n์„ ์•„๋Š๋ƒ print("๊ฐ•ํ•œ์นœ๊ตฌ ๋Œ€ํ•œ์œก๊ตฐ\n๊ฐ•ํ•œ์นœ๊ตฌ ๋Œ€ํ•œ์œก๊ตฐ") 3๋ฒˆ ๊ณ ์–‘์ด ์ถœ๋ ฅ \ /\ ) ( ') ( / ) \(__)| ํ•ต์‹ฌ \๋ฅผ ์ถœ๋ ฅํ•˜๋ ค๋ฉด ์•ž์— \ํ•œ๋ฒˆ ๋” ๋ถ™์—ฌ์„œ \\์ด๋ ‡๊ฒŒ ์จ์•ผ๋œ๋‹ค! print("\\ /\\\n ) ( ')\n( / )\n \\(__)|") 4๋ฒˆ ๊ฐœ ์ถœ๋ ฅ ํ•ต์‹ฌ์€ "์™€ \๋ฅผ ์–ด๋–ป๊ฒŒ ์ถœ๋ ฅํ•˜๋Š” ๊ฐ€ ๋ฐฉ๋ฒ•์€ ์•ž์—\๋ฅผ ๋ถ™์—ฌ์ฃผ๋ฉด ๋œ๋‹ค! print("|\\_/|\n|q p| /}\n( 0 )\"\"\"\\\n|\"^\"` |\n||_/=\\\\__|") 5๋ฒˆ ์ •์ˆ˜ A์™€ B๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ  A+B๋ฅผ ์ถœ๋ ฅํ•˜๋ผ - ๊ณ„์† ์ปดํŒŒ์ผ ์—๋Ÿฌ ๋œจ๋”๋‹ˆ ๊ฐ‘์ž๊ธฐ ๋œ๋‹ค. ์˜ˆ์ œ ์ž…๋ ฅ :1 2 ์ถœ๋ ฅ.. 2020. 1. 13.
728x90
๋ฐ˜์‘ํ˜•