Skip to content

Commit

Permalink
Merge pull request #45 from AlgorithmWithMe/heejik/9week
Browse files Browse the repository at this point in the history
[장희직] Java vs C++, 1,2,3 더하기 4, 도어맨, 배열 복원하기, 후위 표기식2
  • Loading branch information
jhg3410 authored Nov 13, 2022
2 parents 0ee8aa6 + eca9b28 commit a0b2c26
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/main/kotlin/heejik/9week/1, 2, 3 더하기 4.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package heejik.`9week`

// 답보풀: https://ku-hug.tistory.com/m/209

class `1, 2, 3 더하기 4` {

fun solve() {

val cnt = MutableList(10001) { 1 }

for (i in 2..10000) {
cnt[i] += cnt[i-2]
}

for (i in 3..10000) {
cnt[i] += cnt[i-3]
}
repeat(readln().toInt()) {
println(cnt[readln().toInt()])
}
}
}

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

class `Java vs C++` {

data class JAVA(val variable: String) {
var cpp = ""
fun toCPP(): String {
variable.forEach { c ->
if (c.isUpperCase()) {
cpp += '_'
cpp += c.lowercaseChar()
} else {
cpp += c
}
}
return cpp
}
}

data class CPP(val variable: String) {
var java = ""
fun toJava(): String {
var isUpper = false
variable.forEach { c ->
if (c == '_') {
isUpper = true
} else if (isUpper) {
java += c.uppercaseChar()
isUpper = false
} else {
java += c
}
}
return java
}
}

fun solve() {
readln().run {
if (this.first().isLetter().not() || this.first().isLowerCase().not() || this.last() == '_' || this.contains("__")) {
println("Error!")
} else if (this.contains('_') && this.none { it.isUpperCase() }) { // CPP asd_asd
println(CPP(this).toJava())
} else if (this.contains('_').not()){ // JAVA
println(JAVA(this).toCPP())
} else {
println("Error!")
}
}
}
}


fun main() {
`Java vs C++`().solve()
}
49 changes: 49 additions & 0 deletions src/main/kotlin/heejik/9week/도어맨.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package heejik.`9week`

class 도어맨 {


fun solve() {

val x = readln().toInt()
var answer = 0
val order = readln()
var wCount = 0
var mCount = 0
var isChanged = false

kotlin.run {
order.forEachIndexed { index, _c ->
var c = _c
if (isChanged) {
isChanged = false
c = c.change()
}
if (c == 'W') {
if (wCount - mCount == x) {
if (index == order.length - 1 || order[index + 1] == 'W') return@run
isChanged = true
mCount++
} else wCount++
} else {
if (mCount - wCount == x) {
if (index == order.length - 1 || order[index + 1] == 'M') return@run
isChanged = true
wCount++
} else mCount++
}
answer++
}
}
println(answer)
}

private fun Char.change() = if (this == 'W') 'M' else 'W'

}

fun main() {

도어맨().solve()

}
38 changes: 38 additions & 0 deletions src/main/kotlin/heejik/9week/배열 복원하기.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package heejik.`9week`

class `배열 복원하기` {

fun solve() {

val (h, w, x, y) = readln().split(' ').map { it.toInt() }

val a = MutableList(h) { MutableList(w) { 0 } }
val b = mutableListOf<MutableList<Int>>()

repeat(h + x) {
b.add(readln().split(' ').map { it.toInt() }.toMutableList())
}

for (i in 0 until h) {
for (j in 0 until w) {
if (i >= x && j >= y) {
a[i][j] = b[i][j] - a[i-x][j-y]
}
else {
a[i][j] = b[i][j]
}
}
}
a.forEach { row ->
row.forEach { value ->
print("$value ")
}
println()
}
}
}


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

class `후위 표기식2` {

val operands = mutableListOf<Double>()

fun solve() {
val n = readln().toInt()
var postfix = readln().chunked(1).toMutableList()

repeat(n) { cnt ->
val num = readln()
postfix = postfix.map {
if (it.contains('A' + cnt)) num
else it
}.toMutableList()
}

postfix.forEach {
if (it.toIntOrNull() != null) {
operands.add(it.toDouble())
} else {
val a = operands.removeLast()
val b = operands.removeLast()
when (it.first()) {
'*' -> operands.add(b * a)
'+' -> operands.add(b + a)
'/' -> operands.add(b / a)
'-' -> operands.add(b - a)
}
}
}
println(String.format("%.2f", operands.first()))
}
}

fun main() {
`후위 표기식2`().solve()
}

0 comments on commit a0b2c26

Please sign in to comment.