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

[소병희] Java vs C++, 1,2,3 더하기 4, 도어맨, 배열 복원하기, 후위 표기식2 #44

Merged
merged 5 commits into from
Nov 13, 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
44 changes: 44 additions & 0 deletions src/main/kotlin/byeonghee/9week/1 2 3 더하기 4.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package byeonghee.`9week`

import java.io.*

class 소병희_123더하기4 {
companion object {
const val LAST_ONE = 0
const val LAST_TWO = 1
const val LAST_THREE = 2

val br = BufferedReader(InputStreamReader(System.`in`))
val dp = Array(10004) { intArrayOf(1, 0, 0) }
var tc = 0

fun solve() {
val t = br.readLine().toInt()

repeat(t) {
tc = br.readLine().toInt()
println(findCases(tc + 3, LAST_THREE))
}
}

fun findCases(n: Int, e: Int): Int {
if (n <= e) return 0
if (n == 1) return if (e == LAST_ONE) 1 else 0
if (n == 2) return if (e <= LAST_TWO) 1 else 0
if (n == 3) return if (e <= LAST_THREE) 1 else 0

if (dp[n][e] > 0) return dp[n][e]

var ans = 0
for(i in 0..e) {
ans += findCases(n - e - 1, i)
}
dp[n][e] = ans
return ans
}
}
}

fun main() {
소병희_123더하기4.solve()
}
54 changes: 54 additions & 0 deletions src/main/kotlin/byeonghee/9week/Java vs C++.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package byeonghee.`9week`

import java.io.*
import kotlin.system.exitProcess

class 소병희_java_vs_cpp {
companion object {
const val UNDERSCORE = '_'
Copy link
Member

Choose a reason for hiding this comment

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

상수로 이쁘게 빼서 관리하시는 모습 잘 배웠습니다!

const val ERROR = "Error!"

val br = BufferedReader(InputStreamReader(System.`in`))

fun solve() {
val naming = br.readLine()
val sb = StringBuilder()

if (naming.first().isLowerCase().not() || naming.last() == '_' ||
(naming.contains("[A-Z]".toRegex()) && naming.contains('_'))) {
println("Error!")
exitProcess(0)
}

var prev = ' '
var cur = ' '
sb.append(naming.first())
for (i in 1 until naming.length) {
prev = naming[i-1]
cur = naming[i]
if (prev == UNDERSCORE) {
if (cur.isLowerCase()) {
sb.append(cur.uppercase())
}
else error()
}
else if (cur.isUpperCase()) {
sb.append("_${cur.lowercase()}")
}
Comment on lines +29 to +37
Copy link
Member

Choose a reason for hiding this comment

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

굳이 C++ 랑 JAVA 나누지 않고 이렇게 하는 게 더 좋네요!👍

else if (cur.isLowerCase()) {
sb.append(cur)
}
}
println(sb)
}

fun error() {
println(ERROR)
exitProcess(0)
}
}
}

fun main() {
소병희_java_vs_cpp.solve()
}
30 changes: 30 additions & 0 deletions src/main/kotlin/byeonghee/9week/도어맨.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package byeonghee.`9week`

import java.io.*
import kotlin.math.abs

class 소병희_도어맨 {
companion object {
const val M = 1
const val W = -1

val br = BufferedReader(InputStreamReader(System.`in`))
var count = 0

fun solve() {
val d = br.readLine().toInt()
val wait = br.readLine().map { if (it == 'M') M else W }
wait.indexOfFirst {
count += it
abs(count) > d + 1
Comment on lines +17 to +19
Copy link
Member

Choose a reason for hiding this comment

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

indexOfFirst 신기하네요!! count가 계속 실행된다는 것을 병희님 덕분에 알게 되었습니다

}.let { ans -> when (ans) {
-1 -> if (abs(count) == d + 1) println(wait.size - 1) else println(wait.size)
else -> println(ans - 1)
} }
Comment on lines +20 to +23
Copy link
Member

Choose a reason for hiding this comment

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

로직이 넘 깔끔해서 감탄했습니다!! 차이에서 하나를 더 더한 걸 생각한게 놀라웠어요

}
}
}

fun main() {
소병희_도어맨.solve()
}
28 changes: 28 additions & 0 deletions src/main/kotlin/byeonghee/9week/배열 복원하기.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package byeonghee.`9week`

import java.io.*

class 소병희_배열복원하기 {
companion object {
val br = BufferedReader(InputStreamReader(System.`in`))

lateinit var matrixA : Array<IntArray>
lateinit var matrixB : List<List<Int>>

fun solve() {
val (h, w, x, y) = br.readLine().split(" ").map { it.toInt() }
matrixB = List(h + x) { br.readLine().split(" ").map { it.toInt() } }
matrixA = matrixB.subList(0, h).map { it.subList(0, w).toIntArray() }.toTypedArray()

for (r in x until h) for(c in y until w) {
matrixA[r][c] = matrixB[r][c] - matrixA[r - x][c - y]
}

println(matrixA.joinToString("\n") { it.joinToString(" ")})
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.

joinToString을 두 번 사용해서 출력 형식을 맞춘 부분이 이쁜 것 같습니다!

}
}
}

fun main() {
소병희_배열복원하기.solve()
}
46 changes: 46 additions & 0 deletions src/main/kotlin/byeonghee/9week/후위 표기식2.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package byeonghee.`9week`

import java.io.*

class 소병희_후위표기식2 {
companion object {
const val MUL = '*'
const val DIV = '/'
const val ADD = '+'
const val SUB = '-'

val br = BufferedReader(InputStreamReader(System.`in`))
val st = ArrayDeque<Double>()
val ans = 0L

fun solve() {
val n = br.readLine().toInt()
val expression = br.readLine()
val nums = Array(n) { br.readLine().toDouble() }

expression.forEach {
when (it) {
MUL, DIV, ADD, SUB -> calc(st.removeLast(), st.removeLast(), it)
else -> st.addLast(nums[it - 'A'])
}
}
println(st.last().let{ String.format("%.2f", it) })
}

fun calc(n1: Double, n2: Double, opC: Char) {
Copy link
Member

Choose a reason for hiding this comment

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

ArrayDeque가 이미 calc에서 접근이 가능하기 때문에 굳이 n1, n2인지가 필요없을 것 같아요!!

val op: (Double, Double) -> Double =
when (opC) {
MUL -> { b, a -> a * b }
DIV -> { b, a -> a / b }
ADD -> { b, a -> a + b }
SUB -> { b, a -> a - b }
else -> { _, _ -> 0.0 }
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 +31 to +38
Copy link
Member

Choose a reason for hiding this comment

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

람다함수 멋있슴다!

st.addLast(op(n1, n2))
Comment on lines +31 to +39
Copy link
Member

Choose a reason for hiding this comment

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

람다함수 사용 👍

}
}
}

fun main() {
소병희_후위표기식2.solve()
}