贪心算法
例题一
#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
int n;
while (scanf("%d",&n)!=EOF)
{
int min=0;
int max=0;
if(n%2==0){
min=n/4+(n%4)/2;
max=n/2;
}
cout<<min<<" "<<max<<endl;
}
return 0;
}
例题二
#include <iostream>
#include <cmath>
#include <string>
#include <algorithm>
using namespace std;
struct mi{
int start;
int ending;
};
bool compare1(mi x,mi y){
return x.ending<y.ending;
}
int main()
{
int M;
while (scanf("%d",&M)!=EOF)
{
if(M==0){
break;
}
mi a[M];
for(int j=0;j<M;j++){
scanf("%d %d",&a[j].start,&a[j].ending);
}
sort(a,a+M,compare1);
int ans=1;
int time=a[0].ending;
for(int j=1;j<M;j++){
if(time<=a[j].start){
ans++;
time=a[j].ending;
}
}
printf("%d\n",ans);
}
return 0;
}