From 87f40532eae6864b8397e9eeb829772deddf5a77 Mon Sep 17 00:00:00 2001 From: Hykilpikonna Date: Sun, 16 May 2021 22:51:09 +0800 Subject: [PATCH] =?UTF-8?q?[+]=20=E4=B9=8B=E5=89=8D=E5=BF=98=E8=AE=B0?= =?UTF-8?q?=E6=8A=8A=20bills=20=E9=82=A3=E9=81=93=E9=A2=98=E8=A7=A3?= =?UTF-8?q?=E6=BA=90=E7=A0=81=E6=94=BE=E8=BF=9B=E6=9D=A5=E4=BA=86w?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/org/hydev/experiment/CtfBills.kt | 86 +++++++++++++++++++ .../org/hydev/experiment/CtfBillsTests.kt | 38 ++++++++ 2 files changed, 124 insertions(+) create mode 100644 players/Hykilpikonna/KotlinProject/src/main/kotlin/org/hydev/experiment/CtfBills.kt create mode 100644 players/Hykilpikonna/KotlinProject/src/main/kotlin/org/hydev/experiment/CtfBillsTests.kt diff --git a/players/Hykilpikonna/KotlinProject/src/main/kotlin/org/hydev/experiment/CtfBills.kt b/players/Hykilpikonna/KotlinProject/src/main/kotlin/org/hydev/experiment/CtfBills.kt new file mode 100644 index 0000000..b5cf606 --- /dev/null +++ b/players/Hykilpikonna/KotlinProject/src/main/kotlin/org/hydev/experiment/CtfBills.kt @@ -0,0 +1,86 @@ +package org.hydev.experiment + +import java.io.File +import kotlin.math.pow + +/** + * TODO: Write a description for this class! + * + * @author HyDEV Team (https://github.com/HyDevelop) + * @author Hykilpikonna (https://github.com/hykilpikonna) + * @author Vanilla (https://github.com/VergeDX) + * @since 2020-10-31 21:30 + */ +fun main(args: Array) +{ + + val str = File("./bills.csv").readText().replace("\r\n", "\n") + + var total = 0 + str.split("\n").forEachIndexed { i, line -> + if (line.isEmpty()) return@forEachIndexed + + try + { + val chinese = line.split(",")[0] + val count = line.split(",")[1].toInt() + total += chineseToNum(chinese) * count + } + catch (_: NumberFormatException) { } + } + + println(total) +} + +val chineseNumbers = listOf('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖') +val scientific = mapOf('拾' to 1, '佰' to 2, '仟' to 3, '万' to 4, '亿' to 8, '兆' to 12) +val units = mapOf('元' to 100, '角' to 10, '分' to 1) + +fun chineseToNum(chinese: String): Int +{ + var chinese = chinese + chineseNumbers.forEachIndexed { n, c -> chinese = chinese.replace(c.toString(), n.toString()) } + + var amount = 0 + var pendingAmount = 0 + var digitAmount = 0 + for (c in chinese) // 每个字符 + { + // 是数字 + if (c.isDigit()) + { + // 处理中价格加上这个数 + digitAmount += c.toString().toInt(); + } + + // 是个十百千万 + else if (scientific.containsKey(c)) + { + // 特殊情况: 十在前面不是单位而是 1x + if (c == '拾' && chinese.startsWith("拾")) pendingAmount = 10 + + // 处理中价格乘上 10^x + else + { + pendingAmount += digitAmount * 10.0.pow(scientific[c]!!).toInt() + digitAmount = 0 + } + } + + // 是单位 + else if (units.containsKey(c)) + { + // 结束处理, 添加, 重置 + amount += pendingAmount * units[c]!! + digitAmount * units[c]!! + pendingAmount = 0 + digitAmount = 0 + } + + else + { + println("Unknown: $c") + } + } + + return amount +} diff --git a/players/Hykilpikonna/KotlinProject/src/main/kotlin/org/hydev/experiment/CtfBillsTests.kt b/players/Hykilpikonna/KotlinProject/src/main/kotlin/org/hydev/experiment/CtfBillsTests.kt new file mode 100644 index 0000000..50f9b4d --- /dev/null +++ b/players/Hykilpikonna/KotlinProject/src/main/kotlin/org/hydev/experiment/CtfBillsTests.kt @@ -0,0 +1,38 @@ +package org.hydev.experiment + +import org.junit.Test + +/** + * TODO: Write a description for this class! + * + * @author HyDEV Team (https://github.com/HyDevelop) + * @author Hykilpikonna (https://github.com/hykilpikonna) + * @author Vanilla (https://github.com/VergeDX) + * @since 2020-10-31 22:15 + */ +class CtfBillsTests +{ + @Test + fun test715() + { + test("柒元壹角伍分", 715) + } + + @Test + fun test1046() + { + test("拾元肆角陆分", 1046) + } + + @Test + fun test15573() + { + test("壹佰伍拾柒元柒角叁分", 15773) + } + + fun test(s: String, i: Int) + { + print(chineseToNum(s)) + assert(chineseToNum(s) == i) + } +}