程序代码:
#include <stdio.h>
#include <stdlib.h>
#define M 120000
struct Node
{
int a,b;
}p[M];
int n;
int cmp(const void *a,const void *b)
{
return ((struct Node*)a)->a-((struct Node*)b)->a;
}
void Calc()
{
qsort(p+1,n,sizeof(p[0]),cmp);
FILE *f=fopen("2.txt","w");
int i,Pre=1,Max=p[1].b;
for (i=2; i<=n; i++)
if (p[i].a<=Max)
{
if (Max<p[i].b) Max=p[i].b;
}
else
{
fprintf(f,"%d %d\n",p[Pre].a,Max);
Pre=i;
Max=p[i].b;
}
fprintf(f,"%d %d\n",p[Pre].a,Max);
fclose(f);
}
void Init()
{
FILE *f=fopen("1.txt","r");
char s[30];
n=0;
while (fscanf(f,"%[^,]",s)!=EOF)
{
sscanf(s,"%d",&p[++n].a);
fgetc(f);
fscanf(f,"%[^,]",s);
sscanf(s,"%d",&p[n].b);
fgetc(f);
}
fclose(f);
}
int main()
{
Init();
Calc();
}