1047. Student List for Course (25)

来源:互联网 发布:matlab求解最优化问题 编辑:程序博客网 时间:2024/06/06 05:28

保存输入结果,按名字排序,再输出到结果集中

#include<iostream>#include<vector>#include<algorithm>#include<string>#pragma warning(disable:4996)using namespace std;struct student {    string name;    vector<int> subject;    bool operator<(const student that)    {        return this->name < that.name;    }};int N, M;vector<student> ccin;//存储输入数据vector<vector<string>> sub;//存储结果int main(){    cin >> N >> M;    ccin.resize(N);    sub.resize(M);    for (int t = 0;t < N;t++)    {        char a[5];        int temp;        scanf("%s %d", a, &temp);        ccin[t].name = a;        ccin[t].subject.resize(temp);        for (int i = 0;i < temp;i++)            scanf("%d", &ccin[t].subject[i]);    }    sort(ccin.begin(), ccin.end());    for (auto x : ccin)    {        for (auto y : x.subject)            sub[y - 1].push_back(x.name);    }    int n = 1;    for (auto x : sub)    {        printf("%d %d\n", n++, x.size());        for (auto y : x)            printf("%s\n", y.c_str());    }}
0 0