用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 生日广州男生祝福语简短