您的位置:首页 > 其它

2015百度笔试程序题兼职递归求解

2015-09-13 09:35 309 查看
题目大意:Alice是个手机发烧友,最近出了一款她十分喜欢的手机。决定兼职赚钱买手机。现在一天有连续n个兼职,她每次可以连续做m个兼职,每连续做m个时会消耗一点体力,她共有k点体力。求这天她最多可能得到的报酬。

输入:第一行(三个整数) n,m,k

            第二行(n个整数 )每个兼职的报酬

输出:可能得到的最大收益

样例输入:7 1 3

                   7 10 7 18 5 33 0

样例输出:61

分析:题意大致可理解为在n个数组中,求最大k个长度为m的子段之和。一下是自己递归求解代码,有不足之处请指正。

#include<iostream>

#include<numeric>

using namespace std;

int jobPay[5000];

int n, m, k;

int highestPayment(int timeBegin, int timeEnd, int workcout)

{

    int mayPayment[2] = {0,0};

    if ((timeEnd - timeBegin) <= workcout*m)

        return accumulate(jobPay + timeBegin, jobPay + timeEnd,0);

    if (workcout==1)

        mayPayment[0] = accumulate(jobPay + timeBegin, jobPay + timeBegin + m,0);

    else

        mayPayment[0] = accumulate(jobPay + timeBegin, jobPay + timeBegin + m, highestPayment(timeBegin + m, timeEnd, workcout - 1));

    

    mayPayment[1]=highestPayment(timeBegin +1, timeEnd, workcout);

    return mayPayment[0]>mayPayment[1] ? mayPayment[0]:mayPayment[1];

}

int main()

{

    cin >> n>> m>>k;

    

    for (int i = 0; i < n; i++)

        cin >> jobPay[i];

    cout << highestPayment(0, n, k);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: