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