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);
}
输入:第一行(三个整数) 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);
}
相关文章推荐
- android SQLite学习手册
- GCD创建单例的方法
- c# switch case语句
- 复杂度之双边滤波算法的原理、流程、实现及效果。
- jdbc 封装
- Ultra-QuickSort(归并排序)
- GCD-ios 多线程
- 一名程序媛的软件开发之路
- 还在用XListView?试试更漂亮的AutoLoadListView吧!
- Dubbo入门实例(二)
- Scala中隐式参数与隐式转换的联合使用实战详解及其在Spark中的应用源码解析之Scala学习笔记-51
- 收集的Android开源框架及其地址
- 写在开始的话
- 【IOS 开发学习总结-OC-7.3】C 语言特性——动态存储与静态存储
- junit单体测试(PowerMockito)二
- eclipse 搭建struts2框架 出现404错误
- 其中一个案例需求:异常流量
- Zookeeper安装与启动
- Noip 备战篇(一)
- Oracle学习笔记20150913pl/sql编程包的写法