在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
热门推荐
1 狗狗下楼训练
3 狗狗训练头盔
6 狗狗训练机构
7 训练狗狗步骤
9 狗狗能训练
10 狗狗基本动作训练方法
11 怎么训练狗狗叼饭盆
12 狗狗一岁了还能训练吗
13 训练狗狗的
14 宁波狗狗训练
15 训练狗狗拉屎
16 教狗狗训练
17 狗狗要怎么训练上厕所
18 狗狗靠的训练