#includestruct Object { int ObjectId; int ObjectStart; int ObjectFinish; }obj[11]; void Sort(Object obj[],int n) { int j; Object temp; int i; for(i=0;i obj[j+1].ObjectFinish) { temp=obj[j]; obj[j]=obj[j+1]; obj[j+1]=temp; } } } } int GreedySelector(Object obj[],int n) { bool a[11]; a[0]=true; int j=0; int count=1; for(int i=1;i =obj[j].ObjectFinish) { a[i]=true; j=i; count++; } else { a[i]=false; } } return count; } void main() { struct Object obj[11]; obj[0].ObjectId=1; obj[0].ObjectStart=1; obj[0].ObjectFinish=4; obj[1].ObjectId=2; obj[1].ObjectStart=3; obj[1].ObjectFinish=5; obj[2].ObjectId=3; obj[2].ObjectStart=0; obj[2].ObjectFinish=6; obj[3].ObjectId=4; obj[3].ObjectStart=5; obj[3].ObjectFinish=7; obj[4].ObjectId=5; obj[4].ObjectStart=3; obj[4].ObjectFinish=8; obj[5].ObjectId=6; obj[5].ObjectStart=5; obj[5].ObjectFinish=9; obj[6].ObjectId=7; obj[6].ObjectStart=6; obj[6].ObjectFinish=10; obj[7].ObjectId=8; obj[7].ObjectStart=8; obj[7].ObjectFinish=11; obj[8].ObjectId=9; obj[8].ObjectStart=8; obj[8].ObjectFinish=12; obj[9].ObjectId=10; obj[9].ObjectStart=2; obj[9].ObjectFinish=13; obj[10].ObjectId=11; obj[10].ObjectStart=12; obj[10].ObjectFinish=14; Sort(obj,11); int sum = GreedySelector(obj,11); printf("最多可以安排%d個活動\t",sum); } 2 #include #define Max 100 typedef struct { int start; int finish; int number; }arrange; arrange plans[Max]; bool a[Max]; void interchange(arrange* m,arrange* n) { arrange temp=*m; *m=*n; *n=temp; } int partition(arrange array[],int p,int q) { int i,j; i=p; j=q+1; while(1) { do i++; while((array[i].finisharray[p].finish)&&(j!=p)); if(i =p[j].finish) { a[i]=true; j=i; count++; } else a[i]=false; } return count; } void main() { int n,i; cout<<"please input the number of arrange:"< >n; for(i=0;i >plans[i].start>>plans[i].finish; plans[i].number=i+1; } quicksort(plans,0,n-1); cout<<"共能舉行"<