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

[장희직] - 민겸 수, 이모티콘 할인 행사 #209

Merged
merged 7 commits into from
Feb 25, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package heejik.`51week`

import java.math.BigInteger
import kotlin.math.sqrt

class `k진수에서 소수 개수 구하기` {
fun solution(n: Int, k: Int): Int {
var answerCount = 0

n.changeToKNumber(k).split('0').forEach {
it.toLongOrNull()?.let {
if (it.isPrimeNumber()) answerCount++
}
}

return answerCount
}


private fun Int.changeToKNumber(k: Int): String {
var tmp = this
val sb = StringBuilder()

while (tmp >= k) {
sb.insert(0, tmp % k)
tmp /= k
}
sb.insert(0, tmp)
return sb.toString()
}

private fun Long.isPrimeNumber(): Boolean {


if (this == 1L) return false
if (this == 2L) return true
if (this % 2 == 0.toLong()) return false

for (i in 3..sqrt(this.toDouble()).toInt()) {
if (this % i == 0.toLong()) return false
}

return true
}
}

fun main() {
`k진수에서 소수 개수 구하기`().solution(
n = 437674, k = 3
)
}
79 changes: 79 additions & 0 deletions src/main/kotlin/heejik/51week/거리두기 확인하기.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package heejik.`51week`

// P -> 응시자 | O -> 빈테이블 | X -> 파티션

class `거리두기 확인하기` {
data class Pos(
val x: Int,
val y: Int,
)

// 위, 오른, 아래, 왼, 오.위, 오.아래, 왼.위, 왼.아래
private val checkDistance = listOf(
Pos(-2, 0),
Pos(0, 2),
Pos(2, 0),
Pos(0, -2),
// -------------
Pos(-1, 1),
Pos(1, 1),
Pos(-1, -1),
Pos(1, -1)
)
private val checkPos = listOf(
listOf(Pos(-1, 0)),
listOf(Pos(0, 1)),
listOf(Pos(1, 0)),
listOf(Pos(0, -1)),
// ------------------
listOf(Pos(-1, 0), Pos(0, 1)),
listOf(Pos(1, 0), Pos(0, 1)),
listOf(Pos(-1, 0), Pos(0, -1)),
listOf(Pos(1, 0), Pos(0, -1))
)


fun solution(places: Array<Array<String>>): IntArray {
val answer: MutableList<Int> = MutableList(size = 5) { -1 }

places.forEachIndexed { order, waitingRoom ->
val isKeepDistance = checkKeepDistance(waitingRoom = waitingRoom.toList())
answer[order] = if (isKeepDistance) 1 else 0
}

return answer.toIntArray()
}


private fun checkKeepDistance(waitingRoom: List<String>): Boolean {
for ((x, row) in waitingRoom.withIndex()) {
for ((y, element) in row.withIndex()) {
if (element == 'P') {

// distance == 2
repeat(checkDistance.size) {
val nx = x + checkDistance[it].x
val ny = y + checkDistance[it].y

if (nx in 0 until 5 && ny in 0 until 5 && waitingRoom[nx][ny] == 'P') {
checkPos[it].forEach { check ->
val checkX = x + check.x
val checkY = y + check.y
if (waitingRoom[checkX][checkY] == 'O') return true
}
}
}

// distance == 1
listOf(Pos(1, 0), Pos(-1, 0), Pos(0, 1), Pos(0, -1)).forEach {
val nx = x + it.x
val ny = y + it.y

if (nx in 0 until 5 && ny in 0 until 5 && waitingRoom[nx][ny] == 'P') return true
}
}
}
}
return false
}
}
54 changes: 54 additions & 0 deletions src/main/kotlin/heejik/51week/거리두기 확인하기2.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package heejik.`51week`

class `거리두기 확인하기2` {

fun solution(places: Array<Array<String>>): IntArray {
val answer = mutableListOf<Int>()

places.forEach { place ->
repeat(5) { x ->
repeat(5) { y ->
if (place[x][y] == 'P') {
if (check(place, 0, x, y, 0).not()) {
answer.add(0)
return@forEach
}
}
}
}
answer.add(1)
}

return answer.toIntArray()
}


fun check(place: Array<String>, depth: Int, x: Int, y: Int, skip: Int): Boolean {
var result = true

if (x < place.lastIndex) {
when (place[x + 1][y]) {
'P' -> return false
'O' -> if (depth == 0) result = check(place, 1, x + 1, y, 0)
}
}

if (result && y - 1 >= 0 && skip != -1) {
when (place[x][y - 1]) {
'P' -> return false
'O' -> if (depth == 0) result = check(place, 1, x, y - 1, 1)
}
}


if (result && y < place.lastIndex && skip != 1) {
when (place[x][y + 1]) {
'P' -> return false
'O' -> if (depth == 0) result = check(place, 1, x, y + 1, -1)
}
}


return result
}
}
62 changes: 62 additions & 0 deletions src/main/kotlin/heejik/51week/민겸 수.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package heejik.`51week`

class `민겸 수` {

fun solve() {
readln().run {
getMaxDecimal().also { println(it) }
getMinDecimal().also { println(it) }
}
}

private fun String.getMaxDecimal(): String {
Copy link
Contributor

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.

확장함수가 예쁘네용

var mStackCount = 0
val decimal = StringBuilder()

this.forEach {
if (it == 'M') mStackCount += 1
if (it == 'K') {
decimal.append(5)
repeat(mStackCount) {
decimal.append(0)
}
mStackCount = 0
}
}
repeat(mStackCount) {
decimal.append(1)
}
return decimal.toString()
}

private fun String.getMinDecimal(): String {
var mStackCount = 0
val decimal = StringBuilder()

this.forEach {
if (it == 'M') mStackCount += 1
if (it == 'K') {
if (mStackCount > 0) {
decimal.append(1)
}
repeat(mStackCount - 1) {
decimal.append(0)
}
decimal.append(5)
mStackCount = 0
}
}

if (mStackCount != 0) {
decimal.append(1)
repeat(mStackCount - 1) {
decimal.append(0)
}
}
return decimal.toString()
}
}

fun main() {
`민겸 수`().solve()
}
81 changes: 81 additions & 0 deletions src/main/kotlin/heejik/51week/양궁대회.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package heejik.`51week`

class 양궁대회 {
var maxDifference = 0

// 점수: 과녁 갯수
private val answerList = MutableList(11) { 0 }
var answer: Map<Int, Int> = mapOf()
fun solution(n: Int, info: IntArray): IntArray {
shot(n, mutableMapOf(), 0, info)

answer.forEach { (key, value) ->
answerList[10 - key] = value
}
return answerList.toIntArray().run {
if (this.all { it == 0 }) intArrayOf(-1) else this
}
}

private fun shot(remainArrow: Int, scoreWithCount: MutableMap<Int, Int>, idx: Int, info: IntArray) {
if (idx == 10 && remainArrow > 0) {
scoreWithCount[0] = remainArrow
shot(remainArrow = 0, scoreWithCount = scoreWithCount, idx = -1, info = info)
scoreWithCount.remove(0)
}
if (remainArrow == 0) {
var apeachScore = 0
info.withIndex().filter { it.value != 0 }.forEach {
if (it.index !in scoreWithCount.keys.map { key -> 10 - key }) {
apeachScore += (10 - it.index)
}
}
val difference = scoreWithCount.keys.sum() - apeachScore
if (difference <= 0) return
if (difference > maxDifference) {
maxDifference = difference
answer = scoreWithCount.toMap()
}
else if (difference == maxDifference) {
for (score in 0..10) {
val isAnswerContain = answer.containsKey(score)
val isScoreWithCountContain = scoreWithCount.containsKey(score)
if (isAnswerContain && isScoreWithCountContain) {
if (scoreWithCount[score]!! > answer[score]!!) {
maxDifference = difference
answer = scoreWithCount.toMap()
}
} else if (isAnswerContain) {
break
} else if (isScoreWithCountContain) {
maxDifference = difference
answer = scoreWithCount.toMap()
}
}
}
return
}
for (i in idx..9) {
if (remainArrow - (info[i] + 1) >= 0) {
scoreWithCount[10 - i] = info[i] + 1
shot(
remainArrow = remainArrow - (info[i] + 1),
scoreWithCount = scoreWithCount,
idx = i + 1,
info = info
)
scoreWithCount.remove(10 - i)
}
}
}
}


fun main() {
양궁대회().solution(
n = 10,
info = intArrayOf(0,0,0,0,0,0,0,0,3,4,3)
).also {
println(it.contentToString())
}
}
Loading