2288. 价格减免

价格减免

解法一: 模拟

go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
func discountPrices(sentence string, discount int) string {
strArr := strings.Split(sentence, " ")
n := len(strArr)
for i := 0; i < n; i++ {
if checkMoney(strArr[i]) {
num, _ := strconv.Atoi(strArr[i][1:])
strArr[i] = fmt.Sprintf("$%.2f", float64(num*100-num*discount)/100.0)
}
}
return strings.Join(strArr, " ")
}

func checkMoney(str string) bool {
n := len(str)
if n < 2 || str[0] != '$' {
return false
}
for i := 1; i < n; i++ {
if str[i] < '0' || str[i] > '9' {
return false
}
}
return true
}
作者

wuhunyu

发布于

2024-06-18

更新于

2025-01-15

许可协议