用C ++减去整数的乘积和数字总和
假设我们有一个数字。我们必须找到数字总和和数字乘积。之后,求和与乘积之差。因此,如果数字为5362,则总和为5+3+6+2=16,而5*3*6*2=180。因此180–16=164
要解决此问题,请取每个数字,然后将其相加并乘以一个,然后返回差值。
示例
让我们看下面的实现以更好地理解-
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int subtractProductAndSum(int n) {
int prod = 1;
int sum = 0;
for(int t = n;t;t/=10){
sum += t % 10;
prod *= t % 10;
}
return prod - sum;
}
};
main(){
Solution ob;
cout << ob.subtractProductAndSum(5362);
}输入项
5362
输出结果
164
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志