活动选择问题(活动安排问题)(最大数目活动选择问题)贪心算法C++实现

本文介绍了如何使用C++编程解决活动选择问题,即在一系列活动中选择最多数量的不相交活动。通过贪心算法,首先按照活动结束时间进行排序,然后逐个检查每个活动,如果它与当前已选择的活动不冲突,则将其加入选择集合。程序展示了如何输入活动数据,执行快速排序和选择活动,并输出结果。
摘要由CSDN通过智能技术生成

// 活动选择问题(活动安排问题)(最大数目活动选择问题).cpp : Defines the entry point for the console application.
//贪心算法

#include "stdafx.h"
#include<iostream>
#define N 100
using namespace std;

struct Activity
{
int number; //活动编号
int begin; //活动开始时间
int end; //活动结束时间
bool flag; //此活动是否被选择
};
//对于活动集,按照结束时间递增排序,使用快速排序
void fast_sort(Activity *act,int f,int t)
{
if(f<t)
{
int i = f-1,j = f;
Activity a = act[t];
while(j<t)
{
if(act[j].end<=a.end)
{
i++;
Activity temp1 = act[i];
act[i] = act[j];
act[j] = temp1;
}
j++;
}
Activity temp2 = act[t];
act[t] = act[i+1];<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值