华为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; }
相关文章推荐
- [华为机试练习题]20.Home+Work
- Home+Work (华为入职前练习)
- 华为OJ 中级 字符串合并处理
- 华为OJ——在字符串中找出连续最长的数字串
- 华为OJ题目(三):字符统计
- 华为OJ-判断两个IP是否在同一子网
- 华为OJ训练-合唱团
- 【华为OJ】统计大写字母个数
- 华为oj最小公倍数
- 华为OJ基础篇-输入n个整数,输出其中最小的k个
- 【华为OJ】iNOC产品部-杨辉三角的变形
- 华为oj 计算字符个数
- 【华为OJ】在字符串中找出连续最长的数字串
- 华为OJ基础篇-蛇形矩阵
- 华为OJ——字符统计
- 华为OJ中级题-简单密码破解
- 华为oj-合唱队
- 华为oj 句子逆序
- 华为oj:字符串加解密
- 华为oj之提取不重复的整数