華為OJ Home+work

來源:互聯網
上載者:User
#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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.