TIL #78

loci·2024년 7월 17일
0

TIL

목록 보기
75/103


백준2446번
해당 그림처럼 출력해야하는 문제


나의풀이

fun main() {
    val n = readLine()!!.toInt()
    var count = 0
    for (i in 2*n-1 downTo 1 step 2) {

        for (j in 1..count) {
            print(" ")
        }
        count++

        for (j in 1..i) {
            print("*")
        }

        println()
    }
    count = n - 2
    for(i in 3..n*2-1 step 2){
        for(j in 1..count){
            print(" ")
        }
        count--
        for(j in 1..i){
            print("*")
        }
        println()
    }

}

다름사람풀이

import java.io.BufferedReader
import java.io.InputStreamReader

fun main() {
    val n = Integer.parseInt(BufferedReader(InputStreamReader(System.`in`)).readLine())
    val sb = StringBuffer()

    for (i in 1 .. (n + (n-1))) {
        val tempI = if(i>n) i - ((i % n) * 2) else i

        for (k in 0 .. (tempI-2)) sb.append(" ")
        for (j in 0 .. ((n-tempI)*2)) sb.append("*")
        sb.append("\n")
    }

    print(sb.toString())
}


백준 10991번
그림과 같이 출력해야한다.

처음에 반칸씩 출력해야하는 줄 알고 어떻게 해야하나 고민했는데 알고보니 한칸씩 사이에 추가한거였다.


나의풀이

fun main() {
    val n = readLine()!!.toInt()
    for(i in 1..n){
        for(j in 1..n-i){
            print(" ")
        }
        for(j in 1..i){
            print("*")
            print(" ")
        }
        println()
    }

}

다른사람의 풀이

import java.io.BufferedReader
import java.io.InputStreamReader

fun main() {
    val n = Integer.parseInt(BufferedReader(InputStreamReader(System.`in`)).readLine())
    val sb = StringBuffer()

    for (i in 1 .. n) {

        for (j in 1 .. n-i) sb.append(" ")
        for (k in 1 .. (i + (i-1))) {
            if (k%2==0) sb.append(" ") else sb.append("*")
        }
        sb.append("\n")
    }

    print(sb.toString())
}

가격에서 1000단위로 콤마(",") 찍어주기

1. DecimalFormat

val decimal = DecimalFormat("#,###")
            val context = holder.itemView.context
            productPrice.text =
                decimal.format(mItems[position].price).toString() + context.getString(R.string.won)
<string name="comma_number">%,d</string>

format에 숫자를 넣어준다.

2. getString만 사용

val context = holder.itemView.context
            productPrice.text = context.getString(R.string.comma_number, mItems[position].price)
<string name="comma_number">%,d원</string>

getString에 string name과 숫자를 넣어주면 자동으로 변환된다.

profile
편리한 개발자

0개의 댓글