自定义结构体:
struct employee
使用结构体:
struct employee e[5]
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct employee
{
int ID;
char name[10];
int age;
};
void paixu(int a[],int N)
{
for(int i=0;i<N;i++)
{
for(int j=i+1;j<N;j++)
{
if(a[i]>a[j])
{
int m=a[i];
a[i]=a[j];
a[j]=m;
}
}
}
}
int search(int num,struct employee e[],int N)
{
int i=0;
for(i=0;i<N;i++)
{
if(e[i].age==num)
{
break;
}
}
return i;
}
int main()
{
int N;
scanf("%d",&N);
struct employee e[N];
for(int i=0;i<N;i++)
{
scanf("%d %s %d",&e[i].ID,e[i].name,&e[i].age);
}
int a[N];
for(int i=0;i<N;i++)
{
a[i]=e[i].age;
}
paixu(a,N);
int length=N>3?3:N;
for(int i=0;i<length;i++)
{
int key=search(a[i],e,N);
printf("%d>%d>%s\n",e[key].age,e[key].ID,e[key].name);
}
}