// 貨幣單位換算
func HW2023010() {
var inputArr: [String] = []
// 測(cè)試用例
// inputArr = ["100CNY"] // 10000
// inputArr = ["3000fen"] // 3000
// inputArr = ["123HKD"] // 10000
// inputArr = ["20CNY53fen", "53HKD87cents"] // 6432
// 開(kāi)始代碼
let _ = readLine()
while let line = readLine() {
inputArr.append(line)
}
var res: Double = 0
for str in inputArr {
var temp = ""
var count: Double = 0 // 每次輸入的貨幣數(shù)初始值為0
for c in str {
if c.isNumber { // 如果當(dāng)前字符是數(shù)字,則添加到字符串temp
temp.append(String(c))
}else {
count += huanSuan(temp, c) // 將數(shù)字轉(zhuǎn)換成fen并累加
temp = "" // 轉(zhuǎn)換完成將字符串temp置空,這樣再經(jīng)過(guò)后面字母字符時(shí)就不進(jìn)行轉(zhuǎn)換了
}
}
res += count // 將每次輸入的貨幣轉(zhuǎn)換成fen,并累加
}
print(Int(floor(res))) // 向下取整打印
}
// numStr:具體錢數(shù)或者空串,str:當(dāng)前正在遍歷的字符
func huanSuan(_ numStr: String, _ str: Character) -> Double {
var tempCount: Double = 0
let num = Double(numStr) ?? 0
if num == 0 { // 表示numStr為空串,說(shuō)明正在遍歷字母不需要轉(zhuǎn)換
}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
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。