快速排序+贪心算法:
--------------------------------------------------------------------------------
#include<stdio.h>
struct Node
{
int start;
int end;
}a[200];
void quick_sort(struct Node *arr, int l, int r)
{
if (l < r)
{
int i = l, j = r, x = arr[l].end;
struct Node temp = arr[l];
while (i < j)
{
while (i < j && x <= arr[j].end)
j--;
if (i < j)
arr[i++] = arr[j];
while (i < j && x > arr[i].end)
i++;
if (i < j)
arr[j--]= arr[i];
}
arr[i] = temp;
quick_sort(arr, l ,i - 1);
quick_sort(arr, i + 1, r);
}
}
int main()
{
int n;
while (scanf("%d",&n) != EOF)
{
int i, j, a1, a2, b1, b2, temp, sum = 1;
for (i = 0; i < n; i++)
{
scanf("%d:%d-%d:%d", &a1, &a2, &b1, &b2);
a2 = a1 * 60 + a2;
b2 = b1 * 60 + b2;
if (a2 > b2)
{
temp = a2;
a2 = b2;
b2 = temp;
}
a[i].start = a2;
a[i].end = b2;
}
quick_sort(a, 0, n-1);
j = 0;
for (i = 0; i < n; i++)
{
if (a[i].start > a[j].end)
{
j = i;
sum++;
}
}
printf("%d\n",sum);
}
return 0;
}
--------------------------------------------------------------------------------
冒泡排序+贪心算法:
--------------------------------------------------------------------------------
#include<stdio.h>
struct Node
{
int start;
int end;
}a[100];
void sort(struct Node *arr, int n)
{
struct Node temp;
int i, j;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - i - 1; j++)
{
if (arr[j].end > arr[j+1].end)
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main()
{
int n,i,j;
while(scanf("%d",&n)!=EOF)
{
int a1,a2,b1,b2,temp,sum=1;
for(i=0;i<n;i++)
{
scanf("%d:%d-%d:%d",&a1,&a2,&b1,&b2);
a2+=a1*60;b2+=b1*60;
if(a2 > b2)
{
temp=a2;
a2=b2;
b2=temp;
}
a[i].start=a2;
a[i].end=b2;
}
sort(a,n);
for(i=0,j=0;i<n;i++)
{
if(a[i].start > a[j].end)
{
j=i;
sum++;
}
}
printf("%d\n",sum);
}
}
--------------------------------------------------------------------------------