在C ++中可以选择除法或考虑的最大值
在本教程中,我们将讨论一个程序,该程序通过选择除法或按原样考虑来找到最大值。
为此,我们将提供一个整数值。我们的任务是通过将数字递归分成四部分或使用给定函数F(n)=max((F(n/2)+F(n/3)+F(n/4)+F(n/5)),n)。
示例
#include <bits/stdc++.h>
using namespace std;
//计算最大结果
int findMaximum(int size) {
int term[size + 1];
term[0] = 0;
term[1] = 1;
int i=2;
while(i <= size) {
term[i] = max(i, (term[i / 2] + term[i / 3] + term[i / 4] + term[i / 5]));
i = i+1;
}
return term[size];
}
int main() {
int number = 37;
cout << "Maximum possible sum: " << findMaximum(number)<< endl;
return 0;
}输出结果
Maximum possible sum: 57