// 货币单位换算
func HW2023010() {
var inputArr: [String] = []
// 测试用例
// inputArr = ["100CNY"] // 10000
// inputArr = ["3000fen"] // 3000
// inputArr = ["123HKD"] // 10000
// inputArr = ["20CNY53fen", "53HKD87cents"] // 6432
// 开始代码
let _ = readLine()
while let line = readLine() {
inputArr.append(line)
}
var res: Double = 0
for str in inputArr {
var temp = ""
var count: Double = 0 // 每次输入的货币数初始值为0
for c in str {
if c.isNumber { // 如果当前字符是数字,则添加到字符串temp
temp.append(String(c))
}else {
count += huanSuan(temp, c) // 将数字转换成fen并累加
temp = "" // 转换完成将字符串temp置空,这样再经过后面字母字符时就不进行转换了
}
}
res += count // 将每次输入的货币转换成fen,并累加
}
print(Int(floor(res))) // 向下取整打印
}
// numStr:具体钱数或者空串,str:当前正在遍历的字符
func huanSuan(_ numStr: String, _ str: Character) -> Double {
var tempCount: Double = 0
let num = Double(numStr) ?? 0
if num == 0 { // 表示numStr为空串,说明正在遍历字母不需要转换
}else if str == "C" {
tempCount += num * 100
}else if str == "H" {
tempCount += num * 10000 / 123
}else if str == "J" {
tempCount += num * 10000 / 1825
}else if str == "E" {
tempCount += num * 10000 / 14
}else if str == "G" {
tempCount += num * 10000 / 12
}else if str == "f" {
tempCount += num * 1
}else if str == "c" {
tempCount += num * 100 / 123
}else if str == "s" {
tempCount += num * 100 / 1825
}else if str == "e" {
tempCount += num * 100 / 14
}else if str == "p" {
tempCount += num * 100 / 12
}
return tempCount
}
网友评论