题目连接:https://www.patest.cn/contests/pat-a-practise/1047这道题和之前的那道很像,我就照着原来的思路去做了,发现做不下去了,因为转为数字的Name根本转不过来了……
查看了其他人的,才恍然大悟:如果vector中没法使用char数组的话,可以构建一个结构体里面只包含char数组!!!
1 #include<stdio.h> 2 #include<vector> 3 #include<map> 4 #include<string> 5 #include<iostream> 6 #include<algorithm> 7 #include<string.h> 8 using namespace std; 9 typedef struct Node 10 { 11 char s[5]; 12 }Name; 13 int cmp(Name a,Name b) 14 { 15 return (strcmp(a.s,b.s)<0); 16 } 17 int main() 18 { 19 int N,K; 20 scanf("%d %d",&N,&K); 21 int i,n,v,id; 22 vector<Name>Course[2505]; 23 char s[5]; 24 for (i=0;i<N;i++) 25 { 26 Name tmp; 27 scanf("%s",tmp.s); 28 scanf("%d",&n); 29 while (n--) 30 { 31 scanf("%d",&v); 32 Course[v].push_back(tmp); 33 } 34 } 35 36 for (i=1;i<=K;i++) 37 { 38 printf("%d %d ",i,Course[i].size()); 39 sort(Course[i].begin(),Course[i].end(),cmp); 40 for (int j=0;j<Course[i].size();j++) 41 { 42 printf("%s ",Course[i][j].s); 43 } 44 } 45 return 0; 46 }