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

[이지민] - 모음사전, 카펫, 반지, 몬스터 트럭, 필터 #3

Merged
merged 6 commits into from
Sep 18, 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
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions src/main/kotlin/jimin/1week/모음사전.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package jimin.`1week`

/*
<문제>
https://school.programmers.co.kr/learn/courses/30/lessons/84512

<구현 방법>
"A", "E", "I", "O", "U", "" 의 배열을 모두 돌 수 있는 6중 for문을 만듬.
mutableSet을 이용하여 중복을 제거해주고, filter를 이용해 길이가 5까지인 숫자만 필터링한 후 정렬하여 몇번째 단어인지 찾음.

<트러블 슈팅>
너무 시간이 많이 걸리는 풀이인 것 같음..

*/

class `모음사전_jimin` {
fun solution(word: String): Int {
return order(word)
}

fun order(word: String): Int {
val list = arrayListOf("A", "E", "I", "O", "U", "")
var total = mutableSetOf<String>()

for (x1 in list) {
for (x2 in list) {
for (x3 in list) {
for (x4 in list) {
for (x5 in list) {
for (x6 in list) {
val newWord = x1 + x2 + x3 + x4 + x5 + x6
total.add(newWord)
}
}
}
}
}
}
val sortedTotal = total.filter { it.length < 6 }.sorted()
//println(sortedTotal)

return sortedTotal.indexOf(word)
}
}
Comment on lines +21 to +44
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.

빈 칸까지 쳐서 for문으로 문자열 만들면 모든 경우를 구할 수 있다는 걸 생각 못했는데 이렇게 하면 되는구나 했어요!

60 changes: 60 additions & 0 deletions src/main/kotlin/jimin/1week/몬스터 트럭.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package jimin.`1week`
/*
<문제>
https://www.acmicpc.net/problem/2897

<구현 방법>
2중 for문으로 주차구역을 다 돌때, 4개의 영역에 #이 포함되어있는지,
X의 개수가 몇개인지를 판별해 주차 가능 공간 개수를 구하였다.

<트러블 슈팅>
array에 특정 값이 포함되어있는 개수를 구하고 싶다면
=> 배열.count { it.contains(찾고자 하는 값) }

*/
fun main() {
val first = readLine()!!.split(" ")
val x = first.first().toInt()
val y = first.last().toInt()
val parking = arrayListOf<ArrayList<String>>()
for (i in 0 until x) {
parking.add(
readLine()!!.split("").drop(1).dropLast(1)
Copy link
Member

Choose a reason for hiding this comment

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

readLine()!! 대신 readln() 을 사용하면 좋을 것 같아요
요기 한번 보셔도 좋을 것 같습니다

as ArrayList<String>
)
}

checkPossibleParkingLot(parking).forEach {
println(it)
}
}

private fun checkPossibleParkingLot(parking: ArrayList<ArrayList<String>>): ArrayList<Int> {
val fourParking = arrayListOf<String>()
val possibleParking = arrayListOf(0, 0, 0, 0, 0)

for (i in 0 until parking.size - 1) {
for (j in 0 until parking[0].size - 1) {
fourParking.add(parking[i][j])
fourParking.add(parking[i + 1][j])
fourParking.add(parking[i][j + 1])
fourParking.add(parking[i + 1][j + 1])

if ("#" in fourParking) {
fourParking.clear()
continue
}
when (fourParking.count { it.contains("X") }) {
Copy link
Member

Choose a reason for hiding this comment

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

고차 함수 잘 활용하신 것 같아요!!

0 -> possibleParking[0] += 1
1 -> possibleParking[1] += 1
2 -> possibleParking[2] += 1
3 -> possibleParking[3] += 1
4 -> possibleParking[4] += 1
}

fourParking.clear()
}
}

return possibleParking
}
56 changes: 56 additions & 0 deletions src/main/kotlin/jimin/1week/반지.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
<문제>
https://www.acmicpc.net/problem/5555

<구현 방법>
연속적으로 같은 개수를 찾아 개수가 찾고자하는 String과 길이가 같으면 true를 반환하게 함.

<트러블 슈팅>
자꾸 8% 쯤에서 틀렸다고 나와서 반례 찾아봄. -> 아래 반례 틀린거 고치니 해결.

ABCDEFGHIJ
1
BCDEFGHIJA

* */

fun main() {
val searchString = readLine()
val num = readLine()?.toInt()
var rings = arrayListOf<String>()
var sum = 0
for (i in 0 until num!!) {
Copy link
Member

Choose a reason for hiding this comment

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

전 num 변수처럼 다음 입력의 개수를 표시하는 친구는 repeat(readln().toInt()) {} 이렇게 사용해요!!

Copy link
Contributor

Choose a reason for hiding this comment

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

num의 non-null처리를 19라인에서 ?: 연산자로 하면 !!를 안 쓸 수 있을 것 같아요!

rings.add(readLine().toString())
val check = searchStrings(searchString.toString(), rings.last())
//println(check)
if (check) sum += 1
}
print(sum)
}

fun searchStrings(search: String, ring: String): Boolean {
//println("case: $ring")
var matchCheck = 0
var tmp = 0
for (i in ring.indices) {
if (ring[i] == search[0]) {
for (j in search.indices) {
tmp = j
if (i + j < ring.length && ring[i + j] == search[j]) {
matchCheck += 1
if (matchCheck == search.length) return true
}else break
}
if (matchCheck != 0){
for (j in tmp until search.length){
if (ring[j - tmp] == search[j]){
matchCheck += 1
}
if (matchCheck == search.length) return true
}
}
}
matchCheck = 0
}
return false
}
44 changes: 44 additions & 0 deletions src/main/kotlin/jimin/1week/카펫.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package jimin.`1week`

/*
<문제>
https://school.programmers.co.kr/learn/courses/30/lessons/42842

<구현 방법>
yellow의 약수를 찾고, 약수들 중에서 brown의 조건을 만족하는 숫자를 찾는다.
이때 약수는 제곱근(sqrt(double or float))을 사용하면 적은 시간 복잡도를 가질 수 있다.

<트러블 슈팅>
IntArray를 바로 출력하면 값이 해시값으로 보임.
=> 배열.contentToString()을 하면 배열이 잘 출력된다.

*/

import kotlin.math.*

class `카펫_jimin` {
fun solution(brown: Int, yellow: Int): IntArray {
var answer = divide(brown, yellow)
answer = answer.sorted().reversed().toIntArray()

return answer
}

private fun divide(brown: Int, yellow: Int): IntArray {
for (i in 1..sqrt(yellow.toDouble()).toInt()) {
if (yellow % i == 0) {
if (brown == (i + 2) * 2 + (yellow / i) * 2) {
return intArrayOf(i + 2, yellow / i + 2)
Comment on lines +28 to +31
Copy link
Member

Choose a reason for hiding this comment

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

👍👍👍

}
}
}
return intArrayOf()
}
}

fun main() {
val test = `카펫_jimin`()
println(test.solution(10,2).contentToString())
println(test.solution(8,1).contentToString())
println(test.solution(24,24).contentToString())
}
46 changes: 46 additions & 0 deletions src/main/kotlin/jimin/1week/필터.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package jimin.`1week`
/*
<문제>
https://www.acmicpc.net/problem/1895

<구현 방법>
전체 픽셀을 2중 for문으로 살펴볼 때 필터의 영역인 3x3 만큼을 정렬하고 중앙값을 구해
해당 값이 T보다 큰 경우를 계산하였다.

<트러블 슈팅>
NumberFormat 에러남.
NumberFormatException : 문자를 숫자로 변경시도하다가 에러가 발생하는 경우.
=> 예제에 불필요한 공백이 있어 오류가 나는 듯함.
trim()을 사용해서 공백을 제거해주니 맞았음.

*/

fun main() {
val x = readLine()!!.trim().split(" ").first().toInt()
val pixel = arrayListOf<ArrayList<Int>>()
for (i in 0 until x) {
pixel.add(readLine()!!.trim().split(" ").map { it.toInt() }
as ArrayList<Int>)
Comment on lines +20 to +23
Copy link
Member

@soopeach soopeach Sep 18, 2022

Choose a reason for hiding this comment

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

pixel을 arrayListOf<MutableList<Int>>()로 사용하시면 22번째에서 입력받은 값(readLine()!! ~~)을 별도로 as ArrayList로 캐스팅하는 대신, toMutableList()로 간단히 형변환을 해줄 수 있어요!!

}

val T = readLine()!!.trim().toInt()

println(checkPixelOverT(pixel, T))
}

private fun checkPixelOverT(pixel: ArrayList<ArrayList<Int>>, T: Int): Int {
val ninePixels = arrayListOf<Int>()
var num = 0
for (i in 0 until pixel.size - 2) {
for (j in 0 until pixel[0].size - 2) {
for (x in 0 until 3) {
for (y in 0 until 3) {
ninePixels.add(pixel[i + x][j + y])
}
}
if (ninePixels.sorted()[4] >= T) num += 1
ninePixels.clear()
}
}
return num
}