Greedy algorithm to solve problems (C language)


foreword

Problem solving:
solve the activity scheduling problem by the greedy method
[Problem description] Suppose there is a set S consisting of 11 activities that need to use a certain resource, S={1,...,11}. This resource can only be occupied by one activity at any time. Activity i has a start time bi and an end time ei (bi<ei), and its execution time is ei-bi, assuming that the earliest activity execution time is 0.
Once an activity starts executing, it cannot be interrupted until it finishes executing. If activity i and activity j have bi≥ej or bj≥ei, the two activities are said to be compatible.
For example, for the n=11 activities in the table below,
the design algorithm seeks an optimal activity arrangement scheme, so that the number of all arranged activities is the largest.
insert image description here


1. Experimental procedures and results

code:

#include<stdio.h>
#include<string.h>

int n;
int b[100];
int e[100];
int cnt=0;
int B[100];
int E[100];

void solve();
void output();
void fun();

int main()
{
    
       int j;
    memset(B,0,sizeof(B));
    memset(E,0,sizeof(E));
	printf("请输入活动的数量:\n");
	scanf("%d",&n);
	fun();
	printf("请输入活动起始时间:\n");
	for(j=0;j<n;j++)
	{
    
    
		printf("请输入第%d个活动起始时间: ",j+1);
		scanf("%d",&b[j]);
	}
	fun();
	printf("请输入活动结束时间:\n");
	for(j=0;j<n;j++)
	{
    
    
		printf("请输入第%d个活动结束时间: ",j+1);
		scanf("%d",&e[j]);
	}
	fun();
	printf("活动名称:");
	for(j=0;j<n;j++)
	{
    
    
		printf("%3d",j+1);
	}
	printf("\n起始时间:");
	for(j=0;j<n;j++)
	{
    
    
		printf("%3d",b[j]);
	}
	printf("\n结束时间:");
	for(j=0;j<n;j++)
	{
    
    
		printf("%3d",e[j]);
	}
	printf("\n");
	fun();
    solve();
    output();
    return 0;
}
void solve()
{
    
    
    int i,j=0;
    B[0]=b[0];
    E[0]=e[0];
    for(i=0;i<=n;i++){
    
    
        if(b[i]>=e[j]){
    
    
            j=i;
            cnt++;
            B[cnt]=b[j];
            E[cnt]=e[j];
        }
    }
}
void output()
{
    
    
    int i;
    printf("求解结果为:\n");
    printf("选取的活动为:\n");
    for(i=0;i<=cnt;i++){
    
    
        printf("第%d个活动:[%d,%d]\n",B[i],E[i]);
    }
    printf("\n共选取%d个活动\n",cnt+1);
}
void fun()
{
    
    
	printf("      ******************************************\n");
}

The result is as follows:
insert image description here

Summarize

This experiment is relatively simple to implement. By creating two arrays, record the start time and end time respectively, and set the greedy algorithm, that is, the end time of the previous activity must be less than the start time of the next activity for selection, and pass two empty arrays Putting in, which greatly reduces the efficiency of using structures, replaces their effects with global variables and empty arrays, but at the same time, due to the lack of the effect of incrementally sorting the activity end time in the revision, there are advantages and disadvantages, you can consider using it before learning The sorting algorithm of this program is worth improving.
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/weixin_51759592/article/details/125864613