算法导论 练习题 16.2-5

xiaoxiao2021-02-28  88

#include <stdio.h> #include <stdlib.h> #define N 5 typedef struct ExtentNode { int x; int y; }Ex,*pEx; int a[N]={1,3,4,6,7}; bool isOverLap(int x,Ex e) { if(e.x<=x && e.y>=x) return true; else return false; } void greedy(pEx e) { e[0].x=a[0]; e[0].y=a[0]+1; for(int i=1,j=0;i<N;i++) { if(!isOverLap(a[i],e[j])) { j++; e[j].x=a[i]; e[j].y=a[i]+1; } } } void printE(pEx e) { for(int i=0;i<N;i++) { if(e[i].x>0) { printf("[%d,%d] ",e[i].x,e[i].y); } } } void main() { pEx e=(pEx)malloc(N*sizeof(Ex)); for(int i=0;i<N;i++) { e[i].x=e[i].y=-1; } greedy(e); printE(e); getchar(); }
转载请注明原文地址: https://www.6miu.com/read-53452.html

最新回复(0)