#include <iostream>
#include <vector>
using namespace std;

vector<int> maxProduct(int n) {
    vector<int> dp(n+1, 0);
    vector<int> res;
    dp[1] = 1;
    for (int i = 2; i <= n; i++) {
        for (int j = 1; j < i; j++) {
            if (j * (i-j) > dp[i]) {
                dp[i] = j * (i-j);
                res.clear();
                res.push_back(j);
            }
            else if (j * (i-j) == dp[i]) {
                res.push_back(j);
            }
        }
    }
    return res;
}

int main() {
    int n;
    cin >> n;
    vector<int> ans = maxProduct(n);
    for (int i = 0; i < ans.size(); i++) {
        cout << ans[i] << " ";
    }
    cout << endl;
    return 0;
}
最大乘积 - C++ 代码实现

原文地址: http://www.cveoy.top/t/topic/f3pL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录