【PAT甲级 vector string排序】1047 Student List for Course (25 分) 含别人的做法
生活随笔
收集整理的這篇文章主要介紹了
【PAT甲级 vector string排序】1047 Student List for Course (25 分) 含别人的做法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目
樣例輸出
題解1 C++
兩個測試用例不通過
#include<iostream> #include<algorithm> #include<string> #define STUSIZE 100 #define COUSIZE 100 using namespace std; class Course { public:string stu[STUSIZE];int size = 0; }; int cmp(string str1, string str2) {return str1 < str2; } int main() {int totalStu, totalCou;cin >> totalStu >> totalCou;Course course[COUSIZE];for (int i = 0; i < totalStu; i++) {string stuName;int stuCouTotal;cin >> stuName >> stuCouTotal;for (int j = 0; j < stuCouTotal; j++) {int couNum;cin >> couNum;course[couNum].stu[course[couNum].size] = stuName;course[couNum].size++;}}for (int i = 0; i < COUSIZE; i++) {if (course[i].size != 0) {sort(course[i].stu, course[i].stu + course[i].size,cmp);}}for (int i = 0; i < COUSIZE; i++) {if (course[i].size != 0) {cout << i << " " << course[i].size << "\n";for (int j = 0; j < course[i].size; j++) {cout << course[i].stu[j] << "\n";}}}system("pause"); }題解2 C++
#include<cstdio> #include<iostream> #include<string> #include<vector> #include<algorithm> using namespace std; const int maxn = 2550; vector<string>course[maxn];int main() {int n, k;scanf("%d%d", &n, &k);for (int i = 1; i <= n; i++) {string name;int c, x;cin >> name >> c;for (int j = 0; j < c; j++) {scanf("%d", &x);course[x].push_back(name);}}for (int i = 1; i <= k; i++) {printf("%d %d\n", i, course[i].size());sort(course[i].begin(), course[i].end());for (int j = 0; j < course[i].size(); j++) {printf("%s\n", course[i][j].c_str());}}return 0; }總結
以上是生活随笔為你收集整理的【PAT甲级 vector string排序】1047 Student List for Course (25 分) 含别人的做法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【PAT甲级】11077 Kuchigu
- 下一篇: 【PAT甲级 删除字符串中重复字母】10