1759: 学生信息插入(武汉科技大学结构体oj)(已AC)

#include<stdio.h>
struct student
{
long no;
char name[9];
int score;
} t;
void input(struct student stu[],int n)
{
int i;
for(i=0;i<n;i++)
{
scanf("%ld %s %d",&stu[i].no,&stu[i].name,&stu[i].score);
}
}
void insert(struct student stu[],int n)
{ int k,i,j;
scanf("%ld %s %d",&t.no,&t.name,&t.score);
for(i=0;i<n;i++)
{
if(t.score >stu[i].score)
{ k=i;
for(j=n-1;j>=i;j--)
{
stu[j+1]=stu[j];
}
stu[k]=t;
break;
}
if(t.score==stu[i].score&&t.no<stu[i].no)
{
k=i;
for(j=n-1;j>=i;j--) stu[j+1]=stu[j];
stu[k]=t; break;
}else if(t.score==stu[i].score&&t.no>stu[i].no)
{
k=i;
for(j=n-1;j>=i;j--) stu[j+1]=stu[j];
stu[k+1]=t;
break;
}
}
if(t.score<stu[n-1].score) stu[n]=t;
}
void print(struct student stu[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%ld %s %d\n",stu[i].no,stu[i].name,stu[i].score);
}
}
int main()
{
int i,n,m;
struct student stu[100];
while(scanf("%d",&n)!=EOF)
{
input(stu,n); //读入n个学生的数据
scanf("%d",&m);
for(i=0;i<m;i++)
{
insert(stu,n); //按照总分降序排序,如果总分相同的学号小的在前面
n++;
}
print(stu,n); //输出n个学生的信息
}
return 0;
}

猜你喜欢

转载自www.cnblogs.com/cy846586184/p/12287692.html