关于【贪心算法】
FatMouse
題目描述:FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.?
The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1's. All integers are not greater than 1000.
For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.
Code: #include <cstdio> #include <algorithm>using namespace std;struct goods{double weight; //該物品總重double totalValue; //該物品總價(jià)值double price; //該物品性價(jià)比 };bool cmp(goods a,goods b){return a.price>=b.price; }int main() {double n; //一共多少錢const int arrSize=1010;int m; //有幾樣物品 goods arr[arrSize];while(scanf("%lf%d",&n,&m)!=EOF){if(n==-1&&m==-1)break;for(int cnt=0;cnt<m;++cnt){scanf("%lf%lf",&arr[cnt].weight,&arr[cnt].totalValue);arr[cnt].price=arr[cnt].weight/arr[cnt].totalValue;}sort(arr,arr+m,cmp);double ans=0;int index=0;while(n>0&&index<m){if(n>arr[index].totalValue){ //若能買下全部該商品ans+=arr[index].weight;n-=arr[index].totalValue;}else{ans+=n/arr[index].totalValue*arr[index].weight;n=0;}++index;}printf("%.3lf\n",ans);}return 0; }/**************************************************************Problem: 1433User: lcyvinoLanguage: C++Result: AcceptedTime:10 msMemory:1020 kb ****************************************************************/
?
今年暑假不AC
題目描述:“今年暑假不AC?”“是的?!薄澳悄愀墒裁茨?#xff1f;”“看世界杯呀,笨蛋!”“@#$%^&*%...”確實(shí)如此,世界杯來了,球迷的節(jié)日也來了,估計(jì)很多ACMer也會(huì)拋開電腦,奔向電視作為球迷,一定想看盡量多的完整的比賽,當(dāng)然,作為新時(shí)代的好青年,你一定還會(huì)看一些其它的節(jié)目,比如新聞聯(lián)播(永遠(yuǎn)不要忘記關(guān)心國家大事)、非常6+7、超級(jí)女生,以及王小丫的《開心辭典》等等,假設(shè)你已經(jīng)知道了所有你喜歡看的電視節(jié)目的轉(zhuǎn)播時(shí)間表,你會(huì)合理安排嗎?(目標(biāo)是能看盡量多的完整節(jié)目)
輸入數(shù)據(jù)包含多個(gè)測(cè)試實(shí)例,每個(gè)測(cè)試實(shí)例的第一行只有一個(gè)整數(shù)n(n<=100),表示你喜歡看的節(jié)目的總數(shù),然后是n行數(shù)據(jù),每行包括兩個(gè)數(shù)據(jù)Ti_s,Ti_e (1<=i<=n),分別表示第i個(gè)節(jié)目的開始和結(jié)束時(shí)間,為了簡化問題,每個(gè)時(shí)間都用一個(gè)正整數(shù)表示。n=0表示輸入結(jié)束,不做處理。
對(duì)于每個(gè)測(cè)試實(shí)例,輸出能完整看到的電視節(jié)目的個(gè)數(shù),每個(gè)測(cè)試實(shí)例的輸出占一行。
Code: #include <cstdio> #include <algorithm>using namespace std;struct program{int startTime;int endTime; };bool cmp(program a,program b){return a.endTime<=b.endTime; }int main() {const int arrSize=110;program arr[arrSize];int n;while(scanf("%d",&n)!=EOF){if(n==0)break;for(int cnt=0;cnt<n;++cnt){scanf("%d%d",&arr[cnt].startTime,&arr[cnt].endTime);}sort(arr,arr+n,cmp);int currentTime=0;int ans=0;for(int index=0;index<n;++index){if(currentTime<=arr[index].startTime){currentTime=arr[index].endTime;++ans;}}printf("%d\n",ans);}return 0; }/**************************************************************Problem: 1434User: lcyvinoLanguage: C++Result: AcceptedTime:10 msMemory:1020 kb ****************************************************************/
?
迷瘴
題目描述:通過懸崖的yifenfei,又面臨著幽谷的考驗(yàn)——
幽谷周圍瘴氣彌漫,靜的可怕,隱約可見地上堆滿了骷髏。由于此處長年不見天日,導(dǎo)致空氣中布滿了毒素,一旦吸入體內(nèi),便會(huì)全身潰爛而死。
幸好yifenfei早有防備,提前備好了解藥材料(各種濃度的萬能藥水)?,F(xiàn)在只需按照配置成不同比例的濃度。
現(xiàn)已知yifenfei隨身攜帶有n種濃度的萬能藥水,體積V都相同,濃度則分別為Pi%。并且知道,針對(duì)當(dāng)時(shí)幽谷的瘴氣情況,只需選擇部分或者全部的萬能藥水,然后配置出濃度不大于 W%的藥水即可解毒。
現(xiàn)在的問題是:如何配置此藥,能得到最大體積的當(dāng)前可用的解藥呢?
特別說明:由于幽谷內(nèi)設(shè)備的限制,只允許把一種已有的藥全部混入另一種之中(即:不能出現(xiàn)對(duì)一種藥只取它的一部分這樣的操作)。
輸入數(shù)據(jù)的第一行是一個(gè)整數(shù)C,表示測(cè)試數(shù)據(jù)的組數(shù);
每組測(cè)試數(shù)據(jù)包含2行,首先一行給出三個(gè)正整數(shù)n,V,W(1<=n,V,W<=100);
接著一行是n個(gè)整數(shù),表示n種藥水的濃度Pi%(1<=Pi<=100)。
對(duì)于每組測(cè)試數(shù)據(jù),請(qǐng)輸出一個(gè)整數(shù)和一個(gè)浮點(diǎn)數(shù);
其中整數(shù)表示解藥的最大體積,浮點(diǎn)數(shù)表示解藥的濃度(四舍五入保留2位小數(shù));
如果不能配出滿足要求的的解藥,則請(qǐng)輸出0 0.00。
Code: #include <cstdio> #include <algorithm>using namespace std;int main() {int C;int n,V,W;const int arrSize=100;int arr[arrSize];while(scanf("%d",&C)!=EOF){for(int cnt=0;cnt<C;++cnt){scanf("%d%d%d",&n,&V,&W);for(int j=0;j<n;++j)scanf("%d",&arr[j]);sort(arr,arr+n);int current_V=0;double current_W=0;for(int k=0;k<n;++k){if(((current_W*current_V+arr[k]*V)/(current_V+V))<=W){current_W=(current_W*current_V+arr[k]*V)/(current_V+V);current_V+=V;}}printf("%d %.2lf\n",current_V,current_W/100);}}return 0; }/**************************************************************Problem: 1435User: lcyvinoLanguage: C++Result: AcceptedTime:0 msMemory:1020 kb ****************************************************************/
?
轉(zhuǎn)載于:https://www.cnblogs.com/Murcielago/p/4188271.html
總結(jié)
- 上一篇: ystem.Windows.Forms.
- 下一篇: JTree用法及JTree使用经验总结转