数字的总和和乘积的最大值,直到C ++中的数字减少到一位数字为止
在本教程中,我们将讨论一个程序,以查找最大的和和数位的乘积,直到数字减少到一位为止
为此,我们将获得一个随机数。我们的任务是找到并打印给定数字的位数的总和与乘积的最大值,直到隐蔽到一位数字为止
示例
#include<bits/stdc++.h>
using namespace std;
//converting number to single digit by adding
long repeatedSum(long n) {
if (n == 0)
return 0;
return (n % 9 == 0) ? 9 : (n % 9);
}
//converting number to single digit by multiplying
long repeatedProduct(long n) {
long prod = 1;
while (n > 0 || prod > 9) {
if (n == 0) {
n = prod;
prod = 1;
}
prod *= n % 10;
n /= 10;
}
return prod;
}
//finding maximum
long maxSumProduct(long N) {
if (N < 10)
return N;
return max(repeatedSum(N), repeatedProduct(N));
}
int main() {
long n = 631;
cout << maxSumProduct(n)<<endl;
return 0;
}输出结果
8
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短