在C ++中查找小于或等于N的数字之间的最大数字乘积
假设我们有一个整数N>0。任务是在小于或等于N的数字中找到数字的最大乘积。如果N为390,则结果为216,因为数字389使最大乘积3*8*9=216。
为了解决这个问题,我们将使用递归方法。因此,如果N=0,则返回1,如果数字N<10,则返回N,否则返回max(max_product(N/10)*(Nmod10),max_product((N/10)-1)*9)
示例
#include<iostream>
using namespace std;
int max_product(int N) {
if (N == 0)
return 1;
if (N < 10)
return N;
return max(max_product(N / 10) * (N % 10), max_product(N / 10 - 1) * 9);
}
int main() {
int N = 432;
cout << "Maximum product is: " << max_product(N);
}输出结果
Maximum product is: 243
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语