贪心学习总结

F
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.
InputThe 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.
OutputFor 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 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1
Sample Output
13.333
31.500

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;

struct A {
    int J;
    int F;
    double B;
} a[1001];

bool cmp (A a,A b)
{
    return a.B>b.B;
}

int main()
{
    int M,N;
    while(cin>>M>>N&&M!=-1&&N!=-1)
    {   double S=0;
        for(int i=0; i<N; i++)
        {
            cin>>a[i].J>>a[i].F;
            a[i].B=a[i].J*1.0/a[i].F;
        }
        sort(a,a+N,cmp); 
        for(int i=0; i<N; i++)
        {
            if(M>=a[i].F)
            {
                S+=a[i].J;
                M-=a[i].F; 
               continue;
            }
            if(M<a[i].F)
            {
                S+=a[i].B*M;
                M=0;
            }
        }
        printf("%0.3lf\n",S); 
    }
    return 0;
}

这是一个十分经典的贪心题,就是按性价比高低排序就行。
但这样一道题我却没能一遍过,浪费许多时间,问题在于自己编写程序的
熟练度还不够。
现在说一下问题
(1)对sort函数不够熟练,在编写排序函数时,我本想类似
return a. x/a. y<b. x<b. y,但因为不熟练,最后只能在
结构体内多定义一个成员来储存性价比。
(2)编程基础不够扎实,从而出现写出的代码不能实现其应有
的操作。如两个整数相除出来的时整数,else的运用经常
出错。
(3)读题不仔细。如最后要保留3位小数,可以用printf实现。
printf("%0.3f",&x);

M
A factory produces products packed in square packets of the same height h and of the sizes 11, 22, 33, 44, 55, 66. These products are always delivered to customers in the square parcels of the same height h as the products have and of the size 66. Because of the expenses it is the interest of the factory as well as of the customer to minimize the number of parcels necessary to deliver the ordered products from the factory to the customer. A good program solving the problem of finding the minimal number of parcels necessary to deliver the given products according to an order would save a lot of money. You are asked to make such a program.
Input
The input file consists of several lines specifying orders. Each line specifies one order. Orders are described by six integers separated by one space representing successively the number of packets of individual size from the smallest size 1
1 to the biggest size 6*6. The end of the input file is indicated by the line containing six zeros.
Output
The output file contains one line for each line in the input file. This line contains the minimal number of parcels into which the order from the corresponding line of the input file can be packed. There is no line in the output file corresponding to the last "null’’ line of the input file.
Sample Input
0 0 4 0 0 1
7 5 1 0 0 0
0 0 0 0 0 0
Sample Output
2
1

#include<iostream>
#include<cmath>
using namespace std;

int main()
{
    int a[7];
    while(cin>>a[1]>>a[2]>>a[3]>>a[4]>>a[5]>>a[6])
    {   if(a[1]==0&&a[2]==0&&a[3]==0&&a[4]==0&&a[5]==0&&a[6]==0)
            break;
        int sum=0,y=0,r=0;
        sum=a[6]+a[5]+a[4]+ceil(double(a[3]*1.0/4));
        y+=11*a[5];
        r+=5*a[4];
        if(a[3]%4==1)
        {
            y+=7;
            r+=5;
        }
        else if(a[3]%4==2)
        {
            y+=6;
            r+=3;
        }
        else if(a[3]%4==3)
        {
            y+=5;
            r+=1;
        }
        if(a[2]>r)
        {
            sum++;
            a[2]-=r;
            while(a[2]>9)
            {
                sum++;
                a[2]-=9;
            }
            y+=36-4*a[2];
        }
        else y+=(r-a[2])*4;
        if(a[1]>y)
        {
            a[1]-=y;
            sum+=ceil(double(a[1]*1.0/36));
        }
        cout<<sum<<endl;
    }
    return 0;
}

这个题怎么说呢。。。这算是我做的有点不同的题。我这个题怎么说呢。。。这算是我做的有点不同的题。我说的不同是指老师课上
没讲过类似的。题意大体就是有一堆包裹,大小有11到66的,要用6*6
的箱子装,问最少用几个箱子。这题的思路也很简单,就是尽可能把每个大
箱子都放满。
说是话,这题没什么好总结的,虽然写出来了,但是使用的最笨的方法。
这次放上来只是保存一种思路。

课上知识点总结
1.memse函数
作用是将某一块内存中的内容全部设置为指定的值,
int a[10];
memset(a,0,sizeof(a));
这样就可以将数组全部赋值为0。
2.优先队列priority_queue
主要用途就是动态排序,每次插入或取出
都会重新排序,使最大的在第一个。

#include<iostream>
#include <queue>
using namespace std;
int main() 
{
    //对于基础类型 默认是大顶堆
    priority_queue<int> a; 
    //等同于 priority_queue<int, vector<int>, less<int> > a;
    
    //             这里一定要有空格,不然成了右移运算符↓
    priority_queue<int, vector<int>, greater<int> > c;  //这样就是小顶堆
    priority_queue<string> b;

    for (int i = 0; i < 5; i++) 
    {
        a.push(i);
        c.push(i);
    }
    while (!a.empty()) 
    {
        cout << a.top() << ' ';
        a.pop();
    } 
    cout << endl;

    while (!c.empty()) 
    {
        cout << c.top() << ' ';
        c.pop();
    }
    cout << endl;

    b.push("abc");
    b.push("abcd");
    b.push("cbd");
    while (!b.empty()) 
    {
        cout << b.top() << ' ';
        b.pop();
    } 
    cout << endl;
    return 0;
}

  1. sort对结构体的排序函数大的在第一个。 3. sort对结构体的排序函数
#include<iostream>
#include<algorithm>
using namespace std;

struct A
{
    int d;
    int s;
} a[1001];

bool cmp(A a,A b)
{
    return a.s>b.s;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值