版权声明: https://blog.csdn.net/hernofogot/article/details/85471477
- 题目:统计成绩:给出n个学生的考试成绩表,每条信息由姓名和分数组成,试设计一个算法:(1)按分数高低次序,打印出每个学生在考试中获得的名次,分数相同的为同一名次;(2)按名次列出每个学生的姓名与分数。
- 冒泡排序
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define MAXSIZE 4
struct student{
char name[8];
int score;
}stu[MAXSIZE];
int main()
{
int i,j,temp1,No=1;
char temp2[MAXSIZE];
printf("请输入学生的姓名与分数:\n");
for(i=0;i<MAXSIZE;i++)
{
printf("姓名:");
scanf("%s",stu[i].name);
getchar();
printf("成绩:");
scanf("%d",&stu[i].score);
}
for(i=0;i<MAXSIZE;i++)
{
for(j=0;j<MAXSIZE-i;j++)
{
if(stu[j].score<stu[j+1].score)
{
temp1=stu[j].score;
stu[j].score=stu[j+1].score;
stu[j+1].score=temp1;
strcpy(temp2,stu[j].name);
strcpy(stu[j].name,stu[j+1].name);
strcpy(stu[j+1].name,temp2);
}
}
}
for(i=0;i<MAXSIZE;i++)
{
if(stu[i].score!=stu[i-1].score) No=i;
else No=No;
printf("第%d名:%s的成绩为%d\n",No+1,stu[i].name,stu[i].score);
}
return 0;
}
- 快速插入:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define MAXSIZE 4
struct student{
char name[8];
int score;
}stu[MAXSIZE];
int main()
{
int i,j,key,No=1;
char temp[MAXSIZE];
printf("请输入学生的姓名与分数:\n");
for(i=0;i<MAXSIZE;i++)
{
printf("姓名:");
scanf("%s",stu[i].name);
getchar();
printf("成绩:");
scanf("%d",&stu[i].score);
}
for(i=1;i<MAXSIZE;i++)
{
if(stu[i].score>stu[i-1].score)
{
key=stu[i].score;
strcpy(temp,stu[i].name);
stu[i].score=stu[i-1].score;
strcpy(stu[i].name,stu[i-1].name);
for(j=i-2;key<stu[j].score;j--)
{
stu[j+1].score=stu[j].score;
strcpy(stu[j+1].name,stu[i].name);
}
stu[j+1].score=key;
strcpy(stu[j+1].name,temp);
}
}
for(i=0;i<MAXSIZE;i++)
{
if(stu[i].score!=stu[i-1].score) No=i;
else No=No;
printf("第%d名:%s的成绩为%d\n",No+1,stu[i].name,stu[i].score);
}
}