pat甲级1012

1012 The Best Rank (25)(25 分)

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 Algebra), 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

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

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,M,E三科分数均为[0, 100]整数,因此可用数组cnt存储每个分数的人数,然后累加就可以得到每个分数对应的名次。而平均分不是整数,先将平均分排序,然后用二分查找的方法找出其对应的名次。
 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 #include <map>
 5 using namespace std;
 6
 7 struct grade
 8 {
 9     int s[3];
10     double A;
11 };
12 int cnt[3][101];
13 int cnt2[3][101];
14
15 int avgRank(vector<double> avg, double d);
16
17 int main()
18 {
19     int n, m;
20     cin >> n >> m;
21     map<int, grade> mapp;
22     vector<double> avg;
23
24     int id, i, j;
25     grade g;
26     for (i = 0; i < n; i++)
27     {
28         cin >> id;
29         g.A = 0;
30         for (j = 0; j < 3; j++)
31         {
32             cin >> g.s[j];
33             g.A += (double)g.s[j];
34             cnt[j][g.s[j]]++;
35         }
36         g.A /= 3;
37         avg.push_back(g.A);
38         mapp[id] = g;
39     }
40
41     cnt2[0][100] = cnt2[1][100] = cnt2[2][100] = 1;
42     for (i = 99; i >= 0; i--)
43     {
44         for (j = 0; j < 3; j++)
45             cnt2[j][i] = cnt2[j][i + 1] + cnt[j][i + 1];
46     }
47
48     sort(avg.begin(), avg.end());
49
50     int min, s, t;
51     char arr[4] = { ‘C‘, ‘M‘, ‘E‘, ‘A‘ };
52     for (i = 0; i < m; i++)
53     {
54         min = 3000;
55         cin >> id;
56         if (mapp.find(id) == mapp.end())
57         {
58             cout << "N/A" << endl;
59             continue;
60         }
61         for (j = 0; j < 3; j++)
62         {
63             t = cnt2[j][mapp[id].s[j]];
64             if ( t < min)
65             {
66                 min = t;
67                 s = j;
68             }
69         }
70         t = avgRank(avg, mapp[id].A);
71         if (t <= min)
72         {
73             min = t;
74             s = 3;
75         }
76         cout << min << " " << arr[s] << endl;
77     }
78     return 0;
79 }
80
81 int avgRank(vector<double> avg, double d)
82 {
83     int mid = -1, i = 0, j = avg.size() - 1;
84     while (i <= j)
85     {
86         mid = (i + j) / 2;
87         if (d > avg[mid])
88             i = mid + 1;
89         else if (d == avg[mid])
90             break;
91         else
92             j = mid - 1;
93     }
94     return avg.size() - mid;
95 }

提交时发现直接把平均分四舍五入取整也可以通过:

 1 #include <iostream>
 2
 3 #include <map>
 4 using namespace std;
 5
 6 struct grade
 7 {
 8     int s[4];
 9 };
10
11 int cnt[4][101];
12 int cnt2[4][101];
13
14 int main()
15 {
16     int n, m;
17     cin >> n >> m;
18     map<int, grade> mapp;
19
20     int id, i, j;
21     grade g;
22     for (i = 0; i < n; i++)
23     {
24         cin >> id;
25         g.s[0] = 0;
26         for (j = 1; j < 4; j++)
27         {
28             cin >> g.s[j];
29             g.s[0] += g.s[j];
30             cnt[j][g.s[j]]++;
31         }
32         g.s[0] = g.s[0] / 3.0 + 0.5;
33         cnt[0][g.s[0]]++;
34         mapp[id] = g;
35     }
36
37     for (i = 0; i < 4; i++)
38     {
39         cnt2[i][100] = 1;
40         for (j = 99; j >= 0; j--)
41             cnt2[i][j] = cnt2[i][j + 1] + cnt[i][j + 1];
42     }
43     int min, s, t;
44     char arr[4] = { ‘A‘, ‘C‘, ‘M‘, ‘E‘ };
45     for (i = 0; i < m; i++)
46     {
47         min = 3000;
48         cin >> id;
49         if (mapp.find(id) == mapp.end())
50         {
51             cout << "N/A" << endl;
52             continue;
53         }
54         for (j = 0; j < 4; j++)
55         {
56             t = cnt2[j][mapp[id].s[j]];
57             if ( t < min)
58             {
59                 min = t;
60                 s = j;
61             }
62         }
63         cout << min << " " << arr[s] << endl;
64     }
65     return 0;
66 }


原文地址:https://www.cnblogs.com/lxc1910/p/9472632.html

时间: 2024-11-03 01:23:39

pat甲级1012的相关文章

PAT——甲级1012:The Best Rank

1012 The Best Rank (25 point(s)) 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

PAT甲级1012题解——选择一种合适数据存储方式能使题目变得更简单

题目分析: 本题的算法并不复杂,主要是要搞清楚数据的存储方式(选择一种合适的方式存储每个学生的四个成绩很重要)这里由于N的范围为10^6,故选择结构体来存放对应下标为学生的id(N只有2000的范围,所以结构体开10^6其实有点浪费空间),再者对于每个学生的每种成绩的排名我们通过下面的这种方式可以巧妙得到(而且单科成绩是会出现重复的,即并列的情况): 获取排名的方法:其实很简单,由于会出现并列单科成绩的方式,且单个学生的成绩是存储在结构体里的,通过多次排序的方式去解决这道题目是不太合适的,这里用

PAT甲级1005 Spell It Right

题目:PAT甲级 1005 题解:水题.看到题目的第一时间就在想一位一位的mod,最后一加一转换就完事了.结果看到了N最大为10的100的次方,吓得我赶紧放弃这个想法... 发现碰到这种情况用字符串十分好用,这道题应该考察的就是这一点.大致思路就是把数字的每一位放到字符串中,然后通过ASCII码得到每一位的相加结果num,然后把num一位一位的放到stack中,使用stack是因为它先进先出的特性,最后输出就行了. 代码: 1 #include<cstdio> 2 #include<qu

PAT甲级考前整理

终于在考前,刷完PAT甲级130道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种境界.PAT甲级题目总的说卡题目的比较多,卡测试点的比较少,有些题目还会有题意混淆,这点就不吐槽了吧.静下心来耍这130道题,其实磨练的是一种态度与手感,养成的是一种习惯.热爱AC没有错!! 130道题目主要的考点: 1.排序:快速排序,直接插入排序,希尔排序,分治排序,堆排序. 2.图论:拓扑排序.最短路径.深度搜索.广度搜索. 3.树:树的遍历.完全二叉树.AVL. 4.其他:并查集,模拟,哈希.背包.

PAT甲级考试题库1001 A+B Format 代码实现及相关知识学习

准备参加九年九月份的PAT甲级证书考试,对网站上的题目进行总结分析: 1001题 A+B Format (20 分) Calculate a+b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits). 计算a+b的值并以一定格式输出其和sum(数字需要

PAT甲级专题|最短路

PAT甲级最短路 主要算法:dijkstra 求最短最长路.dfs图论搜索. 1018,dijkstra记录路径 + dfs搜索路径最值 25分,错误点暂时找不出.. 如果只用dijkstra没法做,只能得20分 #include<bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 510; int cmax,n,ter,m; int caps[maxn]; int g[maxn][m

【PAT甲级】1070 Mooncake (25 分)(贪心水中水)

题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全部出手的利润.输出最大利润. trick: 测试点2可能包含M不为整数的数据.(尽管题面说明M是正整数,可是根据从前PAT甲级题目的经验,有可能不是整数.....) 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using names

PAT 甲级测试题目 -- 1012 The Best Rank

题目链接 题目描述 输入小于等于 2000 的数据 N,M,分别表示 学生的总数 和 查询学生的数量 输入 N 个学生的六位数 id 以及 C,M,E 三科成绩.输入 M 个查询学生的 id. 要求输出: 若被查询的 id 不存在,输出 N/A 若被查询的 id 存在,输出 C,M,E,A(average 平均分) 四个成绩中排名最高的排名,以及对应的分数类型(C, M, E, A).若有多余一个类型的分数相同,则按照 A > C > M > E 的优先序列输出 排名 和 分数类型 分析

PAT乙级 1012. 数字分类 (20)

1012. 数字分类 (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字: A1 = 能被5整除的数字中所有偶数的和: A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4...: A3 = 被5除后余2的数字的个数: A4 = 被5除后余3的数字的平均数,精确到小数点后1位: A5 = 被5除后余4的数字中最大数