ACM

来源:互联网 发布:数据归一化后的值 编辑:程序博客网 时间:2024/06/07 13:56
杭电1.3.1
Problem Description
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.
 

#include <stdio.h>int main(){int N,i,j;double temp = 0;double M;int out[1002];int get[1002];double og[1002];while(scanf("%lf%d",&M,&N) ){if(M == -1 && N == -1){break;}for(i=0; i<N; i++){scanf("%d%d",&get[i],&out[i]);if(out[i] == 0){og[i] = 100001.0;}else{og[i] = get[i] * 1.0 / out[i];}}for(i=0; i<N-1; i++){for(j=i+1; j<N; j++){if(og[i] < og[j]){temp = og[i];og[i] = og[j];og[j] = temp;temp = get[i];get[i] = get[j];get[j] = temp;temp = out[i];out[i] = out[j];get[j] = temp;}}}double sum = 0;temp = 0;for(i=0; i<N; i++){temp = temp + out[i];sum = sum + get[i];j = i;if(temp > M){sum = sum - get[i]; //减去最后一个房子,因为最后一个房子不能全部兑换break;}}sum = sum + (M - (temp - out[j])) * og[j];printf("%.3lf\n",sum);}return 0;}





0 0