PAT-A1012.The Best Rank 【排序】
2018-01-27 11:00
260 查看
思路:
1、用结构体数组保存输入数据
2、分别按4个指标排序,并在过程中记录最佳名次和科目
3、把结构体数组元素加入map,ID作为键值,这样便于查询,不然要遍历数组
1、用结构体数组保存输入数据
2、分别按4个指标排序,并在过程中记录最佳名次和科目
3、把结构体数组元素加入map,ID作为键值,这样便于查询,不然要遍历数组
/************************** //@Author: 3stone //@ACM: PAT-A1012.The Best Rank //@Time: 18/1/27 //@IDE: VS2017 ***************************/ #include<cstdio> #include<iostream> #include<algorithm> #include<string> #include<cstring> #include<map> using namespace std; typedef struct { string id; int C, M, E, A; int bestRank; char bestCourse; }Stu; bool cmp1(Stu s1, Stu s2) { if (s1.C != s2.C) return s1.C > s2.C; else return s1.id < s2.id; } bool cmp2(Stu s1, Stu s2) { if (s1.M != s2.M) return s1.M > s2.M; else return s1.id < s2.id; } bool cmp3(Stu s1, Stu s2) { if (s1.E != s2.E) return s1.E > s2.E; else return s1.id < s2.id; } bool cmp4(Stu s1, Stu s2) { if (s1.A != s2.A) return s1.A > s2.A; else return s1.id < s2.id; } void mySort(Stu mp[], int n) { //按A排序 sort(mp, mp + n, cmp4); int rank = 1; mp[0].bestRank = rank; //A优先级最高,直接赋值,后边用严格 < 判断 mp[0].bestCourse = 'A'; for (int i = 1; i <= n; i++) { if (mp[i].A != mp[i - 1].A) rank = i + 1; mp[i].bestRank = rank; mp[i].bestCourse = 'A'; } //按C排序 sort(mp, mp + n, cmp1); rank = 1; if (rank < mp[0].bestRank) { //边排序边判断,是否是最佳名次 mp[0].bestRank = rank; mp[0].bestCourse = 'C'; } for (int i = 1; i <= n; i++) { if (mp[i].C != mp[i - 1].C) rank = i + 1; if (rank < mp[i].bestRank) { mp[i].bestRank = rank; mp[i].bestCourse = 'C'; } } //按M排序 sort(mp, mp + n, cmp2); rank = 1; if (rank < mp[0].bestRank) { mp[0].bestRank = rank; mp[0].bestCourse = 'M'; } for (int i = 1; i <= n; i++) { if (mp[i].M != mp[i - 1].M) rank = i + 1; if (rank < mp[i].bestRank) { mp[i].bestRank = rank; mp[i].bestCourse = 'M'; } } //按E排序 sort(mp, mp + n, cmp3); rank = 1; if (rank < mp[0].bestRank) { mp[0].bestRank = rank; mp[0].bestCourse = 'E'; } for (int i = 1; i <= n; i++) { if (mp[i].E != mp[i - 1].E) rank = i + 1; if (rank < mp[i].bestRank) { mp[i].bestRank = rank; mp[i].bestCourse = 'E'; } } } int main() { Stu mp[2010]; int n, m; scanf("%d%d", &n, &m); char tempStr[10]; for (int i = 0; i < n; i++) {//input scanf("%s", tempStr); mp[i].id = tempStr; scanf("%d %d %d", &mp[i].C, &mp[i].M, &mp[i].E); mp[i].A = round( (double)(mp[i].C + mp[i].M + mp[i].E) / 3 ); } mySort(mp, n); //再加入map map<string, Stu> data; string str; for (int i = 0; i < n; i++) { data[mp[i].id] = mp[i]; } for (int i = 0; i < m; i++) { scanf("%s", tempStr); string str = tempStr; if (data.find(str) == data.end()) { printf("N/A\n"); continue; } printf("%d %c\n", data[str].bestRank, data[str].bestCourse); } system("pause"); return 0; }
相关文章推荐
- PAT甲级-1012. The Best Rank (25)并列排序
- PAT甲题题解-1012. The Best Rank (25)-排序水题
- PAT - 甲级 - 1012. The Best Rank (25)(排序)
- 【PAT】A1012. The Best Rank(结构体、排序)
- PAT 1012. The Best Rank (25) 一定范围的整数值排序
- PAT 1012. The Best Rank (25) 数据结构,排序
- PAT--1012. The Best Rank(排序)
- PAT (Advanced Level) 1012. The Best Rank (25) 最佳排名,结构体排序,哈希查询
- PAT-A1012. The Best Rank (25)(排序)
- 浙大PAT 1012题 1012. The Best Rank
- PAT1012. The Best Rank (25)
- PAT 1012. The Best Rank
- PAT 1012. The Best Rank (25)
- PAT(Advanced lecel) 1012. The Best Rank
- 【PAT Advanced Level】1012. The Best Rank (25)
- PAT(A) - 1012. The Best Rank (25)
- 【PAT】【Advanced Level】1012. The Best Rank (25)
- 浙大PAT (Advanced Level) Practise 1012 The Best Rank (25)
- pat 1012. The Best Rank (25)
- PAT_1012. The Best Rank