今年暑假不AC(水题)

虽然这道题水的不行,但是虽然做了不少题,但是贪心一直是自己的一个BUG,今天稍微做了点,作为理解贪心的一个开始...

贪心的特点性质见博客http://blog.csdn.net/u014665013/article/details/40748115

打开题目链接http://acm.hdu.edu.cn/showproblem.php?pid=2037

#include <iostream>
#include <string>
#include<algorithm>
using namespace std;

struct T{
	int start,end;
}arr[110];


bool compare(T a,T b)
{
	return a.end<b.end;   //升序排列,如果改为return a>b,则为降序
}

int main (){
	int n;
	while(scanf("%d",&n)&&n!=0){
		int i=0;
		int temp=n;
		while(n--){
			scanf("%d%d",&arr[i].start,&arr[i].end);
			i++;
		}
		sort(arr,arr+temp,compare);
		
		int count=1,mem=arr[0].end;
		for(int j=1;j<temp;j++)
			if(arr[j].start>=mem)
			{
				count++,mem=arr[j].end;
			}

			printf("%d
",count);
	}
	return 0;
}


 

原文地址:https://www.cnblogs.com/zswbky/p/5432049.html