結果
問題 |
No.297 カードの数式
|
ユーザー |
![]() |
提出日時 | 2020-10-07 21:49:22 |
言語 | Kotlin (2.1.0) |
結果 |
AC
|
実行時間 | 341 ms / 1,000 ms |
コード長 | 1,905 bytes |
コンパイル時間 | 16,009 ms |
コンパイル使用メモリ | 442,076 KB |
実行使用メモリ | 60,236 KB |
最終ジャッジ日時 | 2024-07-20 04:12:01 |
合計ジャッジ時間 | 26,509 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 23 |
コンパイルメッセージ
Main.kt:75:13: warning: 'appendln(String?): kotlin.text.StringBuilder /* = java.lang.StringBuilder */' is deprecated. Use appendLine instead. Note that the new method always appends the line feed character '\n' regardless of the system line separator. builder.appendln("$max $min") ^
ソースコード
fun main() { val builder = StringBuilder() val n = readInputLine().toInt() var plusCnt = 0 var minusCnt = 0 val numCnt = IntArray(10) readInputLine().split(" ").forEach { when(it) { "+" -> plusCnt++ "-" -> minusCnt++ else -> numCnt[it.toInt()]++ } } val digitCnt = n - plusCnt - minusCnt val allTermList = mutableListOf<Long>() var maxTermTmp = 0L var maxTermLen = 0 val numCntTmp = numCnt.copyOf() for (i in 9 downTo 0) { while (numCntTmp[i] != 0) { if (maxTermLen < digitCnt - plusCnt - minusCnt) { maxTermTmp = maxTermTmp * 10L + i.toLong() maxTermLen++ if (maxTermLen == digitCnt - plusCnt - minusCnt) { allTermList.add(maxTermTmp) } } else { allTermList.add(i.toLong()) } numCntTmp[i]-- } } allTermList.sortDescending() var max = 0L for ((i, t) in allTermList.withIndex()) { if (i <= plusCnt) { max += t } else { max -= t } } var min = 0L if (minusCnt != 0) { for ((i, t) in allTermList.withIndex()) { if (i < minusCnt) { min -= t } else { min += t } } } else { val allTerm = LongArray(plusCnt + 1) var currentIndex = 0 for (i in 0..9) { while (numCnt[i] != 0) { allTerm[currentIndex] = allTerm[currentIndex] * 10L + i.toLong() currentIndex = (currentIndex + 1) % (plusCnt + 1) numCnt[i]-- } } min = allTerm.sum() } builder.appendln("$max $min") print(builder.toString()) } fun readInputLine(): String { return readLine()!! }