Home+Work (华为入职前练习)

来源:互联网 发布:淘宝在线客服咨询 编辑:程序博客网 时间:2024/05/17 22:07

题目描述

临近开学了,小C才想起来数学老师布置了暑假作业。暑假作业是很多张试卷,每张试卷所需的时间和获取的价值已知,请你帮他安排一下,用他仅剩的一点时间来做最有价值的作业。

 

接口说明

原型:

int GetMaxValue(int nPapers, int nRemain, int paper[][2], double* pMaxValue) 

输入参数:

int  nPapers:试卷的数目(1Papers20)

int  nRemain:表示剩余的时间(1nRemain10000)

int  paper[][2]:nPapers*2的数组,每一行的两个元素依次为做完这一份试卷所需的时间、做完这份试卷获取的价值。如果剩余时间不够做完一份卷子,可根据剩余时间获得卷子的部分价值。

输出参数:

double * pMaxValue:获得的最大价值

返回值:

0:异常,1:成功

 

#include "stdafx.h"#include "iostream"#include "string"using namespace std;/*输入: nPapers表示试卷的数目(1≤Papers≤20),nRemain表示剩余的时间(1≤nRemain≤10000),paper[][2]是一个Papers*2的数组,每一行的两个元素依次为做完这一份试卷所需的时间、做完这份试卷的价值输出: *pMaxValue为获得的最大价值返回:0:异常1:计算成功返回*/int GetMaxValue(int nPapers, int nRemain, int paper[][2], double* pMaxValue){if (nPapers<0 || nPapers>200 || nRemain<0 || nRemain>10000){return 0;}*pMaxValue=0;int i,index,timesum=0,timesumMax=0;double temp[20]={0};for (i=0;i<nPapers;i++){temp[i]=(double)paper[i][1]/(double)paper[i][0];timesumMax+=paper[i][0];}do{double f=0;for (i=0;i<nPapers;i++){if (f<temp[i]){f=temp[i];index=i;}}temp[index]=0;timesum+=paper[index][0];if (timesum<nRemain){*pMaxValue+=paper[index][1];}else{double t=(double)paper[index][1]*(nRemain-timesum+paper[index][0])/(double)paper[index][0];*pMaxValue+=t;}} while (timesum<nRemain && timesum<timesumMax);return 1;}int main(){while(true){int paper[5][2]={{4,20},{4,10},{5,22},{10,3},{1,2}};double a=0;double* pMaxValue=&a;int n;cin>>n;GetMaxValue(5, n, paper, pMaxValue);cout<<*pMaxValue<<endl;}return 0;}


 

0 0
原创粉丝点击