题目链接:1281. 整数的各位积和之差 - 力扣(LeetCode)

题目描述:

这个题十分简单,直接边取各个位上的数然后分别计算积与和即可。时间复杂度:

代码:

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
int subtractProductAndSum(int n) {
int mul = 1, sum = 0;
while (n) {
mul *= n % 10;
sum += n % 10;
n /= 10;
}
return mul - sum;
}
};

评测结果: