查找成绩排名
描述
读入n名学生的姓名、身份证号、成绩。查找并输出匹配查找串的学生的姓名和学号和成绩。学生名单中可能缺考,没有成绩,表示为n/a。
如果查找串是针对姓名或者身份证号,则是模糊查找,即只要其中包含查找串(区分大小写)即算满足条件,如果查找串是针对成绩,则是精确查找。
输入
每个测试输入包含1个测试用例,格式为
第1行:正整数n 待查找串
第2行:第1个学生的姓名 学号 成绩
第3行:第2个学生的姓名 学号 成绩
第n+1行:第n个学生的姓名 学号 成绩
其中姓名为不超过10个字符的字符串,身份证是标准18位,成绩为0到100之间的一个整数。
输出
输出包含相应查找串的学生信息(姓名 学号 成绩 排名)(如果成绩为n/a,则排名就为n/a),项之间用空格隔开。每个学生一行。其中排名是按照成绩降序排列的排名。如果有多个学生的成绩相同,则这些学生的排名相同,并且随后的几个排名将跳过。满足条件的多个学生按照输入的顺序输出。
代码如下:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
typedef struct _student
{
char name[12]; //姓名
char id[20]; //身份证号
int score; //成绩,缺考设为0
int rank; //排名
}Student;
//判断字符是否是数字字符
int isNumber(char c)
{
if (c >= '0' && c <= '9')
return 1;
else
return 0;
}
//解析字符串
void parse(char* buf, Student* s)
{
int i = 0, j = 0;
//过滤前面的空格--以防万一
while (buf[i] != '\0' && buf[i] == ' ')
i++;
//录入名字
while (buf[i] != '\0' && isNumber(buf[i]) == 0)
{
s->name[j] = buf[i];
i++;
j++;
}
//删除多余的空格
s->name[j] = 0;
j--;
while (j >= 0)
{
if (s->name[j] == ' ')
{
s->name[j] = 0;
j--;
}
else
break;
}
//读取身份证
j = 0;
for (; j < 18; j++)
s->id[j] = buf[i++];
s->id[j] = 0;
//过滤空格--以防万一
while (buf[i] != '\0' && buf[i] == ' ')
i++;
s->score = 0;
if (buf[i] == 'n')
return;
while (buf[i] != '\0')
{
s->score = s->score * 10 + (buf[i] - '0');
i++;
}
}
//排序
void bubbleSort(Student stu[], int n)
{
int i, j;
Student t;
for (i = 0; i < n - 1; i++)
{
for (j = 0; j < n - 1 - i; j++)
{
if (stu[i].score < stu[j + 1].score)
{
t = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = t;
}
}
}
}
//排名
void rank(Student stu[], int n)
{
int i = 0;
int rk = 1; //初始排名
stu[0].rank = 1;
for (i = 1; i < n; i++)
{
if (stu[i].score == stu[i - 1].score)
stu[i].rank = stu[i - 1].rank;
else
stu[i].rank = i + 1;
}
}
//判断字符串是否是数字字符串
int isAllNmb(char* p)
{
int s = 0;
int i = 0;
//
while (p[i] != '\0' && isNumber(p[i]))
{
s = s * 10 + (p[i] - '0');
i++;
}
if (p[i] == '\0')
return s;
else
return -1;
}
int main()
{
Student stu[1000];
char tmp[100], ch;
char search[20] = { 0 }; //搜索字符串
int i = 0, n, flag = 0, j = 0;
int sn = 0;
scanf("%d", &n); //读取学生个数
while (1)
{
ch = getchar();
if (ch == '\n')
break;
if (ch == ' ' && flag == 0)
continue;
else
{
flag = 1;
search[j++] = ch;
}
}
search[j] = 0;
//读取后面的数据
for (i = 0; i < n; i++)
{
scanf("%[^\n]%c", tmp, &ch);
parse(tmp, &stu[i]);
}
//排序并排名
bubbleSort(stu, n);
rank(stu, n);
//查找
sn = isAllNmb(search);
if (sn == -1) //非纯数字,在姓名和身份证中查找
{
for (i = 0; i < n; i++)
{
if ((strstr(stu[i].name, search) != NULL) || (strstr(stu[i].id, search) != NULL))
{
printf("%s %s ", stu[i].name, stu[i].id);
if (stu[i].score == 0)
printf("n/a n/a\n");
else
printf("%d %d\n", stu[i].score, stu[i].rank);
}
}
}
else
{
//纯数字,可能是成绩,也可能是身份证号
for (i = 0; i < n; i++)
{
if ((stu[i].score == sn) || (strstr(stu[i].id, search) != NULL))
{
printf("%s %s ", stu[i].name, stu[i].id);
if (stu[i].score == 0)
printf("n/a n/a\n");
else
printf("%d %d\n", stu[i].score, stu[i].rank);
}
}
}
return 0;
}
望采纳
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXN 100
#define NAME_LEN 11
#define ID_LEN 19
struct stu {
char name[NAME_LEN];
char id[ID_LEN];
int score;
};
int cmp(const void *p, const void *q) {
return (*(struct stu *)q).score - (*(struct stu *)p).score;
}
int main() {
int n, i, j, flag;
char str[NAME_LEN];
struct stu student[MAXN], temp[MAXN];
scanf("%d%s", &n, str);
for (i = 0; i < n; i++)
scanf("%s%s%d", student[i].name, student[i].id, &student[i].score);
memcpy(temp, student, sizeof(student));
qsort(temp, n, sizeof(struct stu), cmp);
for (i = 0; i < n; i++) {
flag = 0;
if (strstr(student[i].name, str)) flag = 1;
if (strstr(student[i].id, str)) flag = 1;
if (strstr(student[i].score, str)) flag = 1;
if (flag) {
printf("%s %s ", student[i].name, student[i].id);
if (student[i].score != -1) printf("%d ", student[i].score);
else printf("n/a ");
for (j = 0; j < n; j++)
if (!strcmp(temp[j].id, student[i].id)) break;
if (student[i].score != -1) printf("%d\n", j+1);
else printf("n/a\n");
}
}
return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!