昨晚上一个同学把我喊过去看他在九度OJ上为什么老是WA这道题,我想了半个小时的边界条件都没有想出来。然后让他去HDOJ提交这道题目看看怎么,猜测九度的测试数据可能有问题。意料之中,在HDOJ上顺利的AC了这道题目。今天早上起来之后,自己做了下这道题目。比较简单,只需要排个序,没有特殊的边界条件需要考虑。
正在学习C++的函数对象,所以那这道题练习了下函数对象。一共写了三个版本,有比较函数的,重载小于运算符的和函数对象。全部拿OJ测试通过。
题目URL:http://acm.hdu.edu.cn/showproblem.php?pid=1236
我的AC代码:
#include <iostream> #include <algorithm> #include <stdio.h> using namespace std; const int Max = 1010; struct Student { char name[30]; int score; }s[Max]; int score[10]; bool cmp(const Student &a, const Student &b) { if(a.score > b.score) return true; else if(a.score == b.score && strcmp(a.name, b.name) < 0) return true; else return false; } int main() { int n, m, line, ps, pn, pass; while(scanf("%d", &n) && n) { scanf("%d%d", &m, &line); for(int i(0); i<m; ++i) scanf("%d", score+i); for(int i(0); i<n; ++i) { s[i].score = 0; scanf("%s", s[i].name); scanf("%d", &ps); for(int j(0); j<ps; ++j) { scanf("%d", &pn); s[i].score += score[pn-1]; } } sort(s, s+n, cmp); for(pass=0; pass < n && s[pass].score >= line; ++pass); printf("%d\n", pass); for(int i(0); i<pass; ++i) printf("%s %d\n", s[i].name, s[i].score); } system("pause"); return 0; } 重载小于号的AC版本: #include <iostream> #include <algorithm> #include <stdio.h> using namespace std; const int Max = 1010; struct Student { char name[30]; int score; friend bool operator <(const Student &a, const Student &b) { if(a.score > b.score) return true; else if(a.score == b.score && strcmp(a.name, b.name) < 0) return true; else return false; } }; Student s[Max]; int score[10]; int main() { int n, m, line, ps, pn, pass; while(scanf("%d", &n) && n) { scanf("%d%d", &m, &line); for(int i(0); i<m; ++i) scanf("%d", score+i); for(int i(0); i<n; ++i) { s[i].score = 0; scanf("%s", s[i].name); scanf("%d", &ps); for(int j(0); j<ps; ++j) { scanf("%d", &pn); s[i].score += score[pn-1]; } } sort(s, s+n); for(pass=0; pass < n && s[pass].score >= line; ++pass); printf("%d\n", pass); for(int i(0); i<pass; ++i) printf("%s %d\n", s[i].name, s[i].score); } system("pause"); return 0; }
运用函数对象的AC版本:
#include <iostream> #include <algorithm> #include <stdio.h> using namespace std; const int Max = 1010; struct Student { char name[30]; int score; /* friend bool operator <(const Student &a, const Student &b) { if(a.score > b.score) return true; else if(a.score == b.score && strcmp(a.name, b.name) < 0) return true; else return false; }*/ }; Student s[Max]; int score[10]; class Comp { public: bool operator()(const Student &a, const Student &b) { if(a.score > b.score) return true; else if(a.score == b.score && strcmp(a.name, b.name) < 0) return true; else return false; } }; int main() { int n, m, line, ps, pn, pass; while(scanf("%d", &n) && n) { scanf("%d%d", &m, &line); for(int i(0); i<m; ++i) scanf("%d", score+i); for(int i(0); i<n; ++i) { s[i].score = 0; scanf("%s", s[i].name); scanf("%d", &ps); for(int j(0); j<ps; ++j) { scanf("%d", &pn); s[i].score += score[pn-1]; } } sort(s, s+n, Comp()); for(pass=0; pass < n && s[pass].score >= line; ++pass); printf("%d\n", pass); for(int i(0); i<pass; ++i) printf("%s %d\n", s[i].name, s[i].score); } system("pause"); return 0; }
题目如下:
4 5 25 10 10 12 13 15 CS004 3 5 1 3 CS003 5 2 4 1 3 5 CS002 2 1 2 CS001 3 2 3 5 1 2 40 10 30 CS001 1 2 2 3 20 10 10 10 CS000000000000000001 0 CS000000000000000002 2 1 2 0
3 CS003 60 CS001 37 CS004 37 0 1 CS000000000000000002 20HintHuge input, scanf is recommended.