PTA甲级-1012 The Best Rank (25point(s))(最好的成绩)

首先,先贴柳神的博客

https://www.liuchuo.net/ 这是地址

想要刷好PTA,强烈推荐柳神的博客,和算法笔记

题目原文

To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algrbra), and E - English. At the mean time, we encourage students by emphasizing on their best ranks – that is, among the four ranks with respect to the three courses and the average grade, we print the best rank for each student.

For example, The grades of C, M, E and A - Average of 4 students are given as the following:

StudentID  C  M  E  A
310101     98 85 88 90
310102     70 95 88 84
310103     82 87 94 88
310104     91 91 91 91   

Then the best ranks for all the students are No.1 since the 1st one has done the best in C Programming Language, while the 2nd one in Mathematics, the 3rd one in English, and the last one in average.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 2 numbers N and M (≤2000), which are the total number of students, and the number of students who would check their ranks, respectively. Then N lines follow, each contains a student ID which is a string of 6 digits, followed by the three integer grades (in the range of [0, 100]) of that student in the order of C, M and E. Then there are M lines, each containing a student ID.

Output Specification:

For each of the M students, print in one line the best rank for him/her, and the symbol of the corresponding rank, separated by a space.

The priorities of the ranking methods are ordered as A > C > M > E. Hence if there are two or more ways for a student to obtain the same best rank, output the one with the highest priority.

If a student is not on the grading list, simply output N/A.

Sample Input:

5 6
310101 98 85 88
310102 70 95 88
310103 82 87 94
310104 91 91 91
310105 85 90 90
310101
310102
310103
310104
310105
999999   

Sample Output:

1 C
1 M
1 E
1 A
3 A
N/A

题目大意:

期末考试,考了C语言,数学,英语,总分(这是是要你算的,不是考的)

要你找出学生最好的那门课,如果有学生的课程的排名相同,按照

总分>C语言>数学>英语

代码如下

#include<iostream>
#include<string>
#include<cstring>
using namespace std;
struct Student {
	char id[7];	
	int c;		//C语言的成绩
	int m;		//数学的成绩
	int e;		//英语的成绩
	int sum;	//总分
	int rank = -1;	//最好的排名
	int rank_class = -1;	//用来存储最好成绩的课程的
};
int main(void) {
	Student Arry[2010];
	char Course[6] = { 'C','M','E','A' };
	char id[7];			//用来存储我们要找的那个学生
	int t;				//这是我们要寻找的那个学生的下标
	int N, M;
	scanf("%d%d", &N, &M);
	for (int i = 0; i < N; i++) {
		scanf("%s%d%d%d",&Arry[i].id, &Arry[i].c, &Arry[i].m, &Arry[i].e);
		Arry[i].sum = Arry[i].c + Arry[i].m + Arry[i].e;
	}
	while (M--) {
		scanf("%s", id);
		t = -1;
		for (int i = 0; i < N; i++) {
			if (strcmp(Arry[i].id, id) == 0) {
				t = i;
				break;
			}
		}
		if (t == -1) {
			printf("N/A");
			if (M != 0) {
				printf("\n");
			}
			continue;
		}
		int rank_c, rank_m, rank_e, rank_s;
		rank_c = rank_m = rank_e = rank_s = 1;		//他们的值都是N
		for (int i = 0; i < N; i++) {
			if (Arry[i].c > Arry[t].c) {
				rank_c++;
			}
			if (Arry[i].m > Arry[t].m) {
				rank_m++;
			}
			if (Arry[i].e > Arry[t].e) {
				rank_e++;
			}
			if (Arry[i].sum > Arry[t].sum) {
				rank_s++;
			}
		}

		if (rank_s <= rank_c) {
			Arry[t].rank_class = 3;
			Arry[t].rank = rank_s;
		}
		else {
			Arry[t].rank_class = 0;
			Arry[t].rank = rank_c;
		}

		if (Arry[t].rank > rank_m) {
			Arry[t].rank_class = 1;
			Arry[t].rank = rank_m;
		}
		if (Arry[t].rank > rank_e) {
			Arry[t].rank_class = 2;
			Arry[t].rank = rank_e;
		}
		printf("%d %c", Arry[t].rank, Course[Arry[t].rank_class]);
		if (M != 0) {
			printf("\n");
		}
	}
	return 0;
}

诺神的代码

#include <cstdio>
#include <algorithm>
using namespace std;
struct node {
    int id, best;
    int score[4], rank[4];  //rank,分别是他们的等级
}stu[2005];
int exist[1000000], flag = -1;
bool cmp1(node a, node b) { return a.score[flag] > b.score[flag]; }
int main() {
    int n, m, id;
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; i++) {
        scanf("%d %d %d %d", &stu[i].id, &stu[i].score[1], &stu[i].score[2], &stu[i].score[3]);
        stu[i].score[0] = (stu[i].score[1] + stu[i].score[2] + stu[i].score[3]) / 3.0 + 0.5;    //一个四舍五入
    }
    for (flag = 0; flag <= 3; flag++) {
        sort(stu, stu + n, cmp1);       //一个一个的排序
        stu[0].rank[flag] = 1;
        for (int i = 1; i < n; i++) {
            stu[i].rank[flag] = i + 1;
            if (stu[i].score[flag] == stu[i - 1].score[flag])   //如果有成绩相同的办法
                stu[i].rank[flag] = stu[i - 1].rank[flag];
        }
    }
    for (int i = 0; i < n; i++) {
        exist[stu[i].id] = i + 1;
        stu[i].best = 0;
        int minn = stu[i].rank[0];          //就是选出最小的
        for (int j = 1; j <= 3; j++) {
            if (stu[i].rank[j] < minn) {
                minn = stu[i].rank[j];
                stu[i].best = j;
            }
        }
    }
    char c[5] = { 'A', 'C', 'M', 'E' };
    for (int i = 0; i < m; i++) {
        scanf("%d", &id);
        int temp = exist[id];
        if (temp) {     //如果存在的话
            int best = stu[temp - 1].best;
            printf("%d %c\n", stu[temp - 1].rank[best], c[best]);
        }
        else {
            printf("N/A\n");
        }
    }
    return 0;
}

反思:

我的代码没有婼神简介的原因

诺神的代码高度的抽象化,比如她的学科就统一用了一个score数组,我的还是c,m.e,s

她根据题目给的判断优先级,特意的调正了顺序,减少了代码的逻辑量

她还是用到了sort函数,其实我也一直想用这个的,但是没有想到好的用法,这里诺神,再次教了我.

总结

① 尽量写抽象化的代码,减少代码量

② 在可以通过的前提下,用函数是好的

③ 多思考,多记录。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这里给出 PTA 7-8 成绩统计2 的题目描述和 Python 3 代码实现。 ## 题目描述 本题要求读入 N(1 <= N <= 1000)个成绩,统计其中的优秀(>=85)、良好([70, 85))、及格([60, 70))和不及格(<60)的人数,并输出统计结果。当某一类成绩的人数为0时,不输出该行。 ## 输入格式 输入的第一行为正整数 N,表示输入成绩的个数。 接下来 N 行,每行为一个整数,表示一个成绩。 ## 输出格式 统计结果,四行,每行输出一个整数,分别表示优秀、良好、及格和不及格的人数。 ## 输入样例 ``` 10 60 75 90 55 70 82 100 67 74 80 ``` ## 输出样例 ``` 2 5 2 1 ``` ## 代码实现 ```python n = int(input()) # 输入成绩个数 # 初始化四个计数器,分别统计优秀、良好、及格和不及格的人数 excellent = 0 good = 0 passing = 0 failing = 0 # 循环读入 n 个成绩,并统计各个类别的人数 for i in range(n): score = int(input()) # 读入一个成绩 if score >= 85: excellent += 1 elif score >= 70: good += 1 elif score >= 60: passing += 1 else: failing += 1 # 输出各个类别的人数(如果人数为 0,则不输出该行) if excellent > 0: print(excellent) else: print("N/A") if good > 0: print(good) else: print("N/A") if passing > 0: print(passing) else: print("N/A") if failing > 0: print(failing) else: print("N/A") ``` 代码说明: 1. 首先输入成绩个数 n; 2. 然后初始化四个计数器 excellent、good、passing 和 failing,分别用于统计优秀、良好、及格和不及格的人数,初始值都为 0; 3. 接着使用 for 循环,循环 n 次,每次读入一个成绩 score,并使用 if 语句判断该成绩属于哪个类别,然后累加相应的计数器; 4. 循环结束后,使用 if 语句,判断各个类别的人数是否为 0,如果不为 0,则输出该类别的人数,否则输出 "N/A"。 5. 完成以上步骤,即可得到各个类别的人数统计结果。 希望能帮助到您!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值