PAT-B1058 选择题(20 分)

1058 选择题(20 分)

批改多选题是比较麻烦的事情,本题就请你写个程序帮助老师批改多选题,并且指出哪道题错的人最多。

输入格式:

输入在第一行给出两个正整数 N(≤ 1000)和 M(≤ 100),分别是学生人数和多选题的个数。随后 M 行,每行顺次给出一道题的满分值(不超过 5 的正整数)、选项个数(不少于 2 且不超过 5 的正整数)、正确选项个数(不超过选项个数的正整数)、所有正确选项。注意每题的选项从小写英文字母 a 开始顺次排列。各项间以 1 个空格分隔。最后 N 行,每行给出一个学生的答题情况,其每题答案格式为 (选中的选项个数 选项1 ……),按题目顺序给出。注意:题目保证学生的答题情况是合法的,即不存在选中的选项数超过实际选项数的情况。

输出格式:

按照输入的顺序给出每个学生的得分,每个分数占一行。注意判题时只有选择全部正确才能得到该题的分数。最后一行输出错得最多的题目的错误次数和编号(题目按照输入的顺序从 1 开始编号)。如果有并列,则按编号递增顺序输出。数字间用空格分隔,行首尾不得有多余空格。如果所有题目都没有人错,则在最后一行输出 Too simple

输入样例:

3 4 
3 4 2 a c
2 5 1 b
5 3 2 b c
1 5 4 a b d e
(2 a c) (2 b d) (2 a c) (3 a b e)
(2 a c) (1 b) (2 a b) (4 a b d e)
(2 b d) (1 e) (2 b c) (4 a b c d)

输出样例:

3
6
5
2 2 3 4

分析:

对于一道多选题,保存学生的选择和正确选项,如果二者相同,学生选对,累加分数,否则,将该题目的错误次数加1。

代码:

#include <cstdio>
#include <set>
#include <vector>
#include <map>
#include <string>
#include <algorithm>

using namespace std;

bool cmp(pair<int, int> a, pair<int, int> b)
{
    return a.second == b.second ? a.first < b.first : a.second > b.second;
}
typedef struct item
{
    int id;
    int score;
    set<char> r;
    item(int _id, int _score):id(_id), score(_score){}
}item;
int main()
{
    int N, M, i, j, k;
    vector<item> p;//保存每道题目的序号,分值,正确选项
    map<int, int> mistake;//保存出现错误的题目编号和错误次数
    scanf("%d%d", &N, &M);
    for(i = 0; i < M; i++)
    {   //保存每道题目的信息
        int a, b, c;
        char d;
        scanf("%d%d%d", &a, &b, &c);
        p.push_back(item(i + 1, a));
        for(j = 0; j < c; j++)
        {
            getchar();
            scanf("%c", &d);
            p[i].r.insert(d);
        }
    }
    for(i = 0; i < N; i++)
    {   //遍历每个学生
        int score = 0;
        char tem;
        for(j = 0; j < M; j++)
        {   //遍历一个学生的每道题
            set<char> choice;//记录学生每道题的选择
            int num;//记录学生选择的个数
            string s;//记录出错的题目编号

            getchar();
            scanf("(%d", &num);
            choice.clear();
            for(k = 0; k < num; k++)
            {   //遍历一道题的每个选项
                getchar();
                scanf("%c", &tem);
                choice.insert(tem);
            }
            scanf(")");
            if(choice == p[j].r)
                score += p[j].score;
            else
            {
            	if(mistake.find(j + 1) == mistake.end())
                    mistake[j + 1] = 1;
                else
                    mistake[j + 1]++;
			}          
        }
        printf("%d\n", score);
    }
    if(!mistake.empty())
    {
        vector<pair<int, int>> last(mistake.begin(), mistake.end());//将map中的错误信息拷贝到last中,排序
        sort(last.begin(), last.end(), cmp);
        printf("%d %d", last[0].second, last[0].first);
        for(i = 1; i < last.size(); i++)
        {
            if(last[i].second == last[0].second)
                printf(" %d", last[i].first);
            else
                break;
        }
        printf("\n");
    }
    else
        printf("Too simple\n");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38127801/article/details/81136762