2303.计算应缴税款总额
链接:2303.计算应缴税款总额
难度:Easy
标签:数组、模拟
简介:给你一个整数 income 表示你的总收入。返回你需要缴纳的税款总额。
题解 1 - cpp
- 编辑时间:2023-01-23
- 执行用时:12ms
- 内存消耗:13MB
- 编程语言:cpp
- 解法介绍:遍历。
class Solution {
public:
double calculateTax(vector<vector<int>>& brackets, int income) {
double ans = 0.0;
for (int i = 0, prev = 0; i < brackets.size() && prev <= income; prev = brackets[i++][0])
ans += 1.0 * (min(income, brackets[i][0]) - prev) * brackets[i][1] / 100;
return ans;
}
};
题解 2 - python
- 编辑时间:2023-01-23
- 执行用时:48ms
- 内存消耗:15MB
- 编程语言:python
- 解法介绍:同上。
class Solution:
def calculateTax(self, brackets: List[List[int]], income: int) -> float:
ans = 0.0
prev = 0
for [k, v] in brackets:
if prev > income:
break
ans += (min(income, k) - prev) * v / 100
prev = k
return ans
题解 3 - rust
- 编辑时间:2023-01-23
- 内存消耗:2.2MB
- 编程语言:rust
- 解法介绍:同上。
impl Solution {
pub fn calculate_tax(brackets: Vec<Vec<i32>>, income: i32) -> f64 {
let mut ans = 0f64;
let mut prev = 0;
for item in brackets {
if prev > income {
break;
} else {
ans += ((income.min(item[0]) - prev) * item[1]) as f64 / 100f64;
}
prev = item[0]
}
ans
}
}