• 【C/C++】贪心/算法笔记4.4/PAT B1020月饼/PAT B1023组内最小数


    1. 简单贪心
      所谓简单贪心,就是每步都取最优的一种方法。

    月饼问题:有N种月饼,市场最大需求量D,给出每种月饼的库存量和总售价。
    思路:从贵的往便宜的卖。如果当前的已经卖完了,就卖下一个。如果剩余D不足,就退出。
    知识点:
    对月饼单价排序后保证总数不会变:用结构体存储。

    #include <iostream>
    #include <algorithm>
    using namespace std;
    const int maxn = 1010;
    //创建结构体
    struct mooncake
    {
       double store; //总库存
       double sell; //总售价
       double price; //单价
    }cake[1010];
    
    bool cmp(mooncake a, mooncake b)
    {
       return a.price > b.price;
    }
    
    int main()
    {
       int n;
       double D;
       scanf("%d%lf", &n, &D);
       for(int i = 0; i < n; i++)
       {
          scanf("%lf", &cake[i].store);
       }
       for (int i = 0; i < n; i++)
       {
          scanf("%lf", &cake[i].sell);
          cake[i].price = cake[i].sell/cake[i].store;
       }
       sort(cake, cake + n, cmp);
       double ans = 0; //计算总价
       for (int i = 0; i < n; i++)
       {
          if (D == 0) break;
          if (cake[i].store <= D) //如果库存量小于需求量
          {
             D -= cake[i].store; //将第i种全部卖掉
             ans += cake[i].sell; //将第i种的总售价加上
          }
          else
          {
             ans += cake[i].price * D;
             D = 0;
          }
       }
       printf("%.2f
    ",ans);
       
    }
    

    组个最小数:

    #include <iostream>
    using namespace std;
    
    int main()
    {
       int cnt[10];
       for(int i = 0; i < 10; i++)
       {
          scanf("%d", &cnt[i]);
       }
       //找到第一个不为0的最小数,输出。
       for(int i = 1; i < 10; i++)
       {
          if (cnt[i] > 0)
          {
             printf("%d", i);
             cnt[i]--;
             break;
          }
       }
       //从小到大输出
       for(int i = 0; i < 10; i++)
       {
          for(int j = 0; j < cnt[i]; j++)
          {
             printf("%d", i);
          }
       }
       system("pause");
    }
    
    区间贪心
    左端点从大到小排序
    

    include

    include

    using namespace std;
    const int maxn = 110;

    struct interval
    {
    int x,y;
    }I[maxn];

    bool cmp(interval a, interval b)
    {
    if (a.x == b.x) return a.y < b.y;
    else return a.x > b.x;
    }

    int main()
    {
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
    {
    scanf("%d%d", &I[i].x, &I[i].y);
    }
    sort(I, I+n, cmp); //n组数据
    int ans = 1; //记录不相交区间个数
    int lastX = I[0].x; //第一个必取
    for(int i = 1; i < n; i++)
    {
    if(I[i].y <= lastX)
    {
    lastX = I[i].x;
    ans++;
    }
    }
    printf("%d ", ans);
    system("pause");
    }

    
    右端点从小到大排序
    

    include

    include

    using namespace std;
    const int maxn = 110;

    struct interval
    {
    int x,y;
    }I[maxn];

    bool cmp(interval a, interval b)
    {
    if (a.y == b.y) return a.x > b.x;
    else return a.y < b.y;
    }

    int main()
    {
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++)
    {
    scanf("%d%d", &I[i].x, &I[i].y);
    }
    sort(I, I+n, cmp); //n组数据
    int ans = 1; //记录不相交区间个数
    int lastY = I[0].y; //第一个必取
    for(int i = 1; i < n; i++)
    {
    if (I[i].x >= lastY) ans++;
    lastY = I[i].y;
    }
    printf("%d ", ans);
    system("pause");
    }

  • 相关阅读:
    爱因斯坦谜题的真正答案
    Lucence 中的排序算法解析
    XML 解析中,如何排除控制字符
    如何在百度贴吧里加入自己的广告图片
    ASP.NET 不能调试的几种情况
    Zend_Search_Lucence 中用UTF8 编码建立索引的问题
    c++继承中的内存布局 <转>
    Effective C++ 学习笔记(20)
    Effective C++ 学习笔记(19)
    Effective C++ 学习笔记(17)
  • 原文地址:https://www.cnblogs.com/kinologic/p/14208711.html
Copyright © 2020-2023  润新知