Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[장희직] 킹, 바닥 장식, 빙고, 랜선 자르기, 가로수 #25

Merged
merged 5 commits into from
Oct 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/main/kotlin/heejik/5week/가로수.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package heejik.`5week`

fun main() {

val n = readln().toInt()
val tree = mutableListOf<Int>()
val diff = mutableListOf<Int>()
var gcd = 1

repeat(n) {
val location = readln().toInt()
if (tree.isNotEmpty()) {
diff.add(location - tree.last())
}
Comment on lines +12 to +14
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

입력 받을 때 한번에 거리 계산도 해주신게 인상깊었습니다!

tree.add(location)
}

gcd = getGcd(diff[0], diff[1])
for (i in 2 until diff.size) {
gcd = getGcd(gcd,diff[i])
}

println(((tree.last() - tree.first()) / gcd) + 1 - tree.size)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 처음에 이렇게 접근했었는데..!! 틀렸길래 제 생각이 틀린 줄 알았습니다ㅜㅜ 그런데 다른 곳에서 틀렸었나보네요..!!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

처음이랑 마지막 사이를 나눠주는 거 대박입니다!!


}

fun getGcd(_a: Int, _b: Int): Int {
var a = _a
var b = _b
while (b > 0) {
val tmp = a
a = b
b = tmp % b
}
Comment on lines +30 to +34
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

재귀로 하는 법 밖에 몰랐는데 반복문으로 하는 법 배워갑니다👍

return a
}
46 changes: 46 additions & 0 deletions src/main/kotlin/heejik/5week/랜선 자르기.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package heejik.`5week`

import java.io.BufferedReader
import java.io.InputStreamReader
import kotlin.system.exitProcess

val lines = mutableListOf<Long>()

fun main(): Unit = with(BufferedReader(InputStreamReader(System.`in`))) {
var answer = 0L
val (k, n) = readLine().split(' ').map { it.toInt() }
var s = 0L

repeat(k) {
val line = readLine().toLong()
lines.add(line)
s += line
}

var start = 0L
var end = s / n
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

가능성이 없는 범위는 미리 없애버려서 효율성이 증가할 것 같아요!!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

end값을 이렇게 계산해 넣어주신 게 진짜 좋은 것 같아요!

if (end == 1L) {
println(1)
exitProcess(0)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exitProcess(0) 대신에 그냥 return해도 종료가 됩니다..!!

}
while (start <= end) {
val mid = (end + start) / 2
val cnt = isCorrect(mid)
if (cnt >= n) {
answer = mid
start = mid + 1
} else {
end = mid - 1
}
}

println(answer)
}

fun isCorrect(length: Long): Long {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isCorrect는 뭔가 Boolean을 반환할 것 같은 함수명이네요...!!! 개수를 반환한다는 것을 알 수 있는 함수명은 어떨까요??

var cnt = 0L
lines.forEach {
cnt += it / length
}
return cnt
}
26 changes: 26 additions & 0 deletions src/main/kotlin/heejik/5week/바닥 장식.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package heejik.`5week`

fun main() {
var answer = 0

val (n, m) = readln().split(' ').map { it.toInt() }

val floor = arrayListOf<String>().apply {
repeat(n) {
add(readln())
}
}

repeat(n) { i ->
answer += floor[i].split('|').filter { it.isNotEmpty() }.size
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고차함수 너무 너무 잘 다루시는 것 같아요...!! 배우고 갑니다!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

split 진짜 대박입니다....고차함수 응용왕

}
Comment on lines +14 to +16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이렇게 간단히 풀 수 있다니.. 반성하고 갑니다 👍

repeat(m) { j ->
var tmp = ""
repeat(n) { i ->
tmp += floor[i][j]
}
answer += tmp.split('-').filter { it.isNotEmpty() }.size
}

println(answer)
}
67 changes: 67 additions & 0 deletions src/main/kotlin/heejik/5week/빙고.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package heejik.`5week`

import kotlin.system.exitProcess

val bingo = arrayListOf<MutableList<Int>>()

fun main() {
var cnt = 0
var bingoCnt = 0
val chairMan = arrayListOf<MutableList<Int>>()

repeat(5) {
bingo.add(readln().split(' ').map { it.toInt() }.toMutableList())
}
repeat(5) {
chairMan.add(readln().split(' ').map { it.toInt() }.toMutableList())
}

chairMan.forEachIndexed { _, ints ->
ints.forEachIndexed { _, num ->
bingo.forEachIndexed { _, row ->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_처리 깔끔하고 보기 좋은 것 같아요~

Comment on lines +19 to +21
Copy link
Contributor

@bngsh bngsh Oct 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이거 앞 인자를 다 _로 바꿔주셨으니 forEach로 바꿀 수도 있을 것 같아요...!!

if (num in row) {
cnt += 1
row[row.indexOf(num)] = -1
if (isBingo() >= 3) {
println(cnt)
exitProcess(0)
}
bingoCnt = 0
}
}
}
}
}

fun isBingo(): Int {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요것두 빙고의 개수를 반환한다는 것을 알 수 있는 함수명이 더 잘 어울릴 것 같아요

var cnt = 0
var tmp = 0
repeat(5) { i ->
if (bingo[i].filter { it == -1 }.size == 5) cnt += 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all을 써도 괜찮지 않았을까요??

}

repeat(5) { j ->
tmp = 0
repeat(5) { i ->
if (bingo[i][j] == -1) {
tmp += 1
}
if (tmp == 5) cnt += 1
}
}

tmp = 0
repeat(5) { i ->
if (bingo[i][i] == -1) tmp += 1
}
if (tmp == 5) cnt += 1

tmp = 0
repeat(5) { i ->
if (bingo[i][4 - i] == -1) tmp += 1
}
if (tmp == 5) cnt += 1

return cnt
}

44 changes: 44 additions & 0 deletions src/main/kotlin/heejik/5week/킹.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package heejik.`5week`


val y = listOf('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

리스트로 처리한 게 깔끔한 것 같습니다!!

val move = listOf("R", "L", "B", "T", "RT", "LT", "RB", "LB")
val dx = listOf(0, 0, -1, 1, 1, 1, -1, -1)
val dy = listOf(1, -1, 0, 0, 1, -1, 1, -1)

fun main() {

var (king, rock, n) = readln().split(' ')

repeat(n.toInt()) {
val location = move.indexOf(readln())

var kingX = (king[1].digitToInt()) - 1
var kingY = y.indexOf(king[0])

var rockX = (rock[1].digitToInt()) - 1
var rockY = y.indexOf(rock[0])

val kingNx = kingX + dx[location]
val kingNy = kingY + dy[location]


if ((kingNx in 0..7 && kingNy in 0..7).not()) return@repeat

if (kingNx == rockX && kingNy == rockY) {
val rockNx = rockX + dx[location]
val rockNy = rockY + dy[location]
if ((rockNx in 0..7 && rockNy in 0..7).not()) return@repeat
rockX = rockNx
rockY = rockNy
}
kingX = kingNx
kingY = kingNy

king = (y[kingY].toString()) + (kingX + 1).toString()
rock = (y[rockY].toString()) + (rockX + 1).toString()

}
println(king)
println(rock)
}