soj4236 && soj2750

本文探讨了排序和贪心算法在解决特定问题时的巧妙应用,通过实例分析了两个具有相似解决策略的问题,深入讲解了算法的具体实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

只所以把两个题目放在一块,是觉得这两个题目有异曲同工之妙吧。算是排序和贪心吧。

题目链接:http://cstest.scu.edu.cn/soj/problem.action?id=4263

                     http://cstest.scu.edu.cn/soj/problem.action?id=2750

4263:

#include <stdio.h>
#include <algorithm>
using namespace std;
const int MAX = 1000+10;
struct node
{
    int a;
    int b;
}cloth[MAX];
bool operator<(const node&p1, const node&p2)
{
    return p1.b-p1.a>p2.b-p2.a;
}
int main()
{
    int v, m;
    while (scanf("%d%d", &v, &m) == 2)
    {
        int i;
        for (i=1; i<=m; i++)
        {
            scanf("%d%d", &cloth[i].a, &cloth[i].b);
        }
        sort(cloth+1, cloth+m+1);
        for (i=1; i<=m; i++)
        {
            if (v<=0||v<cloth[i].b)
            {
                puts("NO");
                break;
            }
            else
            {
                v -= cloth[i].a;
            }
        }
        if (i==m+1)
        {
            puts("YES");
        }
    }
}


2750:

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

const int MAX = 10005;
struct node
{
       string str;
}no[MAX];
int n;
bool operator<(const node& a, const node& b)
{
     return a.str + b.str < b.str + a.str;
}
int main()
{
    while (cin >> n)
    {
          for (int i=0; i<n; i++)
          {
              cin >> no[i].str;
          }
          sort(no, no+n);
          for (int i=0; i<n; i++)
          {
              cout << no[i].str;
          }
          cout << endl;
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值