Skip to content

Commit

Permalink
Merge pull request #275 from wellFoundedDevelopers/byeonghee/65week
Browse files Browse the repository at this point in the history
[소병희] - 사과나무, 후위 표기식
  • Loading branch information
bngsh authored Jun 2, 2024
2 parents 99cf4ee + 6473b0e commit ccd6bd3
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/main/kotlin/byeonghee/week65/사과나무.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package byeonghee.week65

class 소병희_사과나무 {
companion object {
fun solve() = with(System.`in`.bufferedReader()) {
val n = readLine().toInt()
val trees = IntArray(n)

var oneCnt = 0 //1 짜리 물뿌리개의 최소 필요개수
var twoCnt = 0 // 2짜리 물뿌리개의 최대 필요개수
var sum = 0

readLine().split(" ").forEachIndexed { i, v ->
trees[i] = v.toInt().also {
if (it % 2 == 1) oneCnt++
if (it >= 2) twoCnt += it / 2
sum += it
}
}

if (sum % 3 > 0 || oneCnt > twoCnt) println("NO")
else println("YES")
}
}
}

fun main() {
소병희_사과나무.solve()
}
46 changes: 46 additions & 0 deletions src/main/kotlin/byeonghee/week65/후위 표기식.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package byeonghee.week65

class 소병희_후위표기식 {
companion object {
val op = charArrayOf('+', '-', '*', '/', '(', ')')

fun solve() = with(System.`in`.bufferedReader()) {
val st = ArrayDeque<Char>()
val sb = StringBuilder()
val pri = HashMap<Char, Int>()
pri.put('+', 1)
pri.put('-', 1)
pri.put('*', 2)
pri.put('/', 2)
pri.put('(', 0)
pri.put(')', 0)

readLine().forEach { i ->
if (i !in op) sb.append(i)
else if (i == '(') st.add(i)
else if (i == ')') {
while (st.isNotEmpty() && st.last() != '(') {
sb.append(st.removeLast())
}
if (st.isNotEmpty()) st.removeLast()
}
else {
while(st.isNotEmpty() && pri[st.last()]!! >= pri[i]!!) {
sb.append(st.removeLast())
}
st.add(i)
}
}

while(st.isNotEmpty()) {
sb.append(st.removeLast())
}

println(sb)
}
}
}

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

0 comments on commit ccd6bd3

Please sign in to comment.