您的位置:首页 > 其它

华为OJ Home+work

2014-05-05 22:58 148 查看
#include "OJ.h"

/*

输入: 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 || nRemain==0 || pMaxValue==0 || paper[0]==0)
return 0;

int CostVersusTime[21]={0}; //记录性价比
int Pos[21]={0};            //性价比排序后,原始的位置也排序
for(int i=0;i<nPapers;i++)
{
CostVersusTime[i]=(*(paper[i]+1))/(*(paper[i]));
}
for(int i=0;i<nPapers;i++)
{
Pos[i]=i;
}

//采用冒泡排序,同时也要修改位置信息
for(int i=0;i<nPapers;i++)
{
for(int j=nPapers-1;j>i+1;j--)
{
if(CostVersusTime[j]>CostVersusTime[j-1])
{
int temp=CostVersusTime[j];
CostVersusTime[j]=CostVersusTime[j-1];
CostVersusTime[j-1]=temp;

Pos[j]=j-1;
Pos[j-1]=j;
}
}
}

*pMaxValue=0;
int m=0;
while(nRemain>0 && m<=nPapers)
{
int temp=Pos[m];
if(nRemain>*paper[temp])//剩余时间可以做一份完整的试卷
{
nRemain-=*paper[temp];
*pMaxValue+=*(paper[temp]+1);
}
else//剩余时间不足以做一份试卷,取得部分价值
{
*pMaxValue+=(nRemain/(*paper[temp]))*(*(paper[temp]+1));
nRemain=0;
}
m++;
}

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