PAT1004题 成绩排名
- 主要使用结构来完成,需要注意的是,结构的初始构造;这个题还是写复杂了,子函数可以进一步简化,需要注意的点标注在代码中
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
#define N 100
struct stu_Score
{
char name[11];
char number[11];
int score;
}student;
int stu_High(int n, stu_Score *student)
{
int high_Num = student[0].score, high_flag = 0;
for (int i = 0; i<n; i++)
{
if (student[i].score >= high_Num)
{
high_Num = student[i].score;
high_flag = i;
}
}
return high_flag;
}
int stu_Low(int n, stu_Score *student)
{
int low_Num = student[0].score, low_flag = 0;
for (int i = 0; i<n; i++)
{
if (student[i].score <= low_Num)
{
low_Num = student[i].score;
low_flag = i;
}
}
return low_flag;
}
int main()
{
int num_op=0;
scanf("%d", &num_op);
stu_Score *student=new stu_Score [N];
student = (struct stu_Score *)malloc(num_op * sizeof(struct stu_Score));
for (int i = 0; i < num_op; i++)
{
scanf("%s %s %d", student[i].name,student[i].number,&student[i].score);
}
int high_num = stu_High(num_op, student);
int low_num = stu_Low(num_op, student);
printf("%s %s\n", student[high_num].name, student[high_num].number);
printf("%s %s\n", student[low_num].name, student[low_num].number);
return 0;
}