poj 1032

    这几天也是做了很多构造的题。有的难想,有的不难想。

题目要构造一个数拆分成一些数的和,使得这些数乘积最大。见代码。

#include <iostream>
using namespace std;

int        n, ans[1000], total;

int main()
{
    int        i;
    bool    first = true;

    //freopen("t.txt", "r", stdin);
    cin >> n;
    i = 2;
    total = 0;
    while (total + i <= n)
    {
        total += i;
        ans[i - 2] = i;
        i++;
    }
    n -= total;
    i = i - 3;
    total = i;
    while (i >= 0 && n > 0)
    {
        n--;
        ans[i]++;
        i--;
    }
    if (n > 0)
        ans[total]++;
    for (i = 0; i <= total; i++)
    {
        if (!first)
            cout << " ";
        first = false;
        cout << ans[i];
    }
    cout <<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/Rainb/p/3880117.html