FatMouse‘ Trade

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. Favorite food JavaBean. The warehouse has N rooms. The first room has J pounds of coffee beans and needs F pounds of cat food. FatMouse does not have to trade all the coffee beans in the room, on the contrary, if he pays F[i] * a% pound of cat food, he may get J[i]* a% pound of coffee beans. Here a is a real number. Now he has assigned this homework for you: tell him the maximum amount of coffee beans he can get .)
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.
(The input consists of multiple test cases. Each test case starts with a line containing two non-negative integers M and N, and then N lines, each line Contains two non-negative integers J[i] and F[i]. The last test case is followed by two 1. 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. (For each test case, print a real number accurate to 3 decimal places in one line, which is the maximum amount of coffee beans that FatMouse can obtain. .)

Code

#include <stdio.h>
#include <stdlib.h>
#define MAX 1000
struct food{
    
    
    int j,f;  || 定义j表示咖啡豆,f表示猫粮。
    double p;  || p表示咖啡豆和猫粮的比例,用双精度更精确。
}food_N[1001];  || 定义个数组表示房间数。
 
int main(void)
{
    
    
    int m,n,i;  || 定义m表示胖鼠有点猫粮数量,n表示总的房间数,i表示房间号。
    while(scanf("%d%d",&m,&n) && (m!=-1 || n!=-1))
    {
    
    
        double sum=0.0;  || 最开始胖鼠是0个咖啡豆。
        struct food temp; 
        for( i=0;i<n;i++)   || 从房间号为0开始循环探索,每次加一。
        {
    
    
            scanf("%d%d",&food_N[i].j,&food_N[i].f);  || 输入j,f 。这里表示第i个房间有j个咖啡豆,且需要f个猫粮兑换。
            food_N[i].p=(double)food_N[i].j/food_N[i].f;  || 第i个房间咖啡豆和需要兑换猫粮的比例。
        }
        for(i=0;i<n;i++)
        {
    
    
            for(int k=i;k<n;k++)  || 定义一个k,k的出现打破了现在的咖啡豆和猫粮比例的排序
            {
    
    
                if(food_N[i].p<food_N[k].p) 
                {
    
    
                    temp=food_N[i];
                    food_N[i]=food_N[k];
                    food_N[k]=temp;  ||这里采用了一个换数的方法更改了排序,也就是冒泡排序。
                }
            }
        }
        for(int i=0;i<n;i++)  
        {
    
    
            if(m>food_N[i].f)  || 在探索到一个房间时,若胖鼠手上的猫粮大于该房间需要兑换的猫粮数。
            {
    
    
                sum+=food_N[i].j;  || 则直接交换。
                m-=food_N[i].f;   || 然后减去兑换的数量。
            }
            else
            {
    
    
                sum+=food_N[i].p*m;  || 若是不够,则按比例兑换。
                break;
            }
        }
        printf("%.3lf\n",sum);
    }
    return 0;
}

Idea
1. Since each room contains the price that the mouse needs to pay, it is necessary to find the largest ratio of coffee beans to cat food for exchange, so that you can get more cat food, so you have to explore each room. Then sort the ratio from largest to smallest. If the final price is not enough to obtain all the coffee beans in the room, the coffee beans corresponding to the price are obtained proportionally.

Note
1. temp is not a keyword of C language, nor is it any function or command word. It is just an ordinary variable defined by the user. Of course, the data type can also be defined by the user at will.
2. Greedy algorithms always make the best choice in the current view. That is to say, the greedy algorithm does not consider the overall optimality, the choice it makes is only a local optimal choice in a certain sense. Of course, I hope that the final result obtained by the greedy algorithm is also the best overall. Although the greedy algorithm cannot obtain the overall optimal solution for all problems, it can produce the overall optimal solution for many problems.

Guess you like

Origin blog.csdn.net/weixin_54501632/article/details/114682986