贪心算法

来源:互联网 发布:淘宝达人入口在哪 编辑:程序博客网 时间:2024/04/29 12:21

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. 

Input

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. 

Output

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. 

Sample Input

5 37 24 35 220 325 1824 1515 10-1 -1

Sample Output

13.33331.500

sort函数要和using namespace std;和头文件联合使用。

贪心的思路。对当前的数据进行排序,然后依次对最大的或者最小的进行操作。

第二周培训A题,很简单。但是runtime error。

runtime error代码

#include <stdio.h>

#include <string.h>
int main()
{
static double M,N,J[101],F[100];
while(scanf("%lf %lf",&M,&N) != EOF)
{
if(M == -1&&N == -1) break;
else
{
double ans = 0;
double JS[101];
for(int i = 0;i < N;i++) 
{
scanf("%lf %lf",&J[i],&F[i]);
JS[i] = J[i]/F[i];
}
//for(int i = 0;i<N;i++)printf("%lf\n",JS[i]);
double max;
int Imax;
max = JS[0];Imax=0;
for(int i = 0;i<N;i++)
{
if(JS[i]>max)
{
max = JS[i];
Imax = i;
}
}
while(M>F[Imax])
{
ans += J[Imax];
M =M - F[Imax];
JS[Imax] = 0;
max = 0;
for(int i = 0;i<N;i++)
{
if(JS[i]>max)
{
max = JS[i];
Imax = i;
}
}
//printf("%lf %d\n",max,Imax);
//printf("%.3lf\n",ans);
}
ans += M*JS[Imax];
printf("%.3lf\n",ans);
}
}
return 0;
}

AC代码(拷的)

  1. #include <iostream>  
  2. #include <cstdio>  
  3. #include <algorithm>  
  4.   
  5. using namespace std;  
  6.   
  7. const int maxn = 10005;  
  8.   
  9. struct W {  
  10.     double get;//收入  
  11.     double pay;//付出的猫食  
  12.     double ave;//收入/付出比  
  13. } w[maxn];  
  14.   
  15. bool cmp(W a, W b) {  
  16.     if (a.ave > b.ave) {  
  17.         return true;  
  18.     }  
  19.   
  20.     return false;  
  21. }  
  22.   
  23. int main() {  
  24.     int n;  
  25.     double m;  
  26.     while (scanf("%lf%d", &m, &n), n != -1 && m != -1) {  
  27.         int i;  
  28.         for (i = 0; i < n; ++i) {  
  29.             scanf("%lf %lf", &w[i].get, &w[i].pay);  
  30.             w[i].ave = w[i].get / w[i].pay;  
  31.         }  
  32.   
  33.         sort(w, w + n, cmp);//贪心,对w按照get/pay进行降序排序  
  34.   
  35.         double sum = 0;  
  36.         i = 0;  
  37. //      while(m >= 0){//不知道为什么这种写法就是不行.  
  38. //          if (m >= w[i].pay) {  
  39. //                          sum = sum + w[i].get;  
  40. //                          m = m - w[i].pay;  
  41. //                      } else {  
  42. //                          sum = sum + w[i].ave * m;  
  43. //                          break;  
  44. //                      }  
  45. //          i++;  
  46. //      }  
  47.   
  48.         for (i = 0; i <= n - 1; i++) {  
  49.             if (m >= w[i].pay) {//如果当前剩余的猫食还足够的话  
  50.                 sum = sum + w[i].get;//那就把那个房间的粮食全部买下  
  51.                 m = m - w[i].pay;//并且手上见去相应的猫食  
  52.             } else {//如果现在手上的猫食已经不够  
  53.                 sum = sum + w[i].ave * m;//那么就按比例拿去一定的猫食  
  54.                 break;  
  55.             }  
  56.         }  
  57.   
  58.         printf("%.3lf\n", sum);  
  59.     }  
  60.   
  61.     return 0;  
  62. }  

0 0
原创粉丝点击