在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