PAT Basic 1058. 选择题(20)(C语言实现)

来源:互联网 发布:linux 查看压缩包格式 编辑:程序博客网 时间:2024/06/06 01:38

题目

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

输入格式:

输入在第一行给出两个正整数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

思路

这道题稍微复杂一些,但是拆分成一个个要点,每一点又是很简单的。

  • 使用结构数组存储每一道题的分数、选项数、正确选项数、正确选项和学生打错数量。
    当然如何存储这些量每个人不尽相同,下面是我的方法:
    • 在一个整型变量中按位存储一道题的正确选项,如答案是a b d e,那么这个量就是二进制0001 1011(从低向高存储),
    • 这样直接比较整型的大小就知道答案是否一致,并且相当于选项数、正确选项数、正确选项三项信息综合在了一起。
  • 读取
    • 读取学生答案的时候,要注意如何处理括号,
    • 发现C语言读取空格分隔的字母还挺麻烦。我是用的while((c = getchar()) != ' ');读到非空白字符。

写出来代码好多,是乙级里代码最多的几题了╮(╯▽╰)╭(难道是我的问题?)

代码

最新代码@github,欢迎来踩 ^_^

#include <stdio.h>#include <stdlib.h>typedef struct prob{    int score;    int answer; /* bitwise storage for at most 5 options */    int wrong;} Prob;/* read 'count option1 ...' format */int readanswer(){    char c;    int count, answer = 0;    scanf("%d", &count);    for(int k = 0; k < count; k++)    {        while((c = getchar()) == ' ');        answer |= 1 << (c - 'a');    }    return answer;}int main(){    int N, M, max = 0, useless;    Prob probs[100];    /* read the answers for each problem */    scanf("%d %d", &N, &M);    for(int i = 0; i < M; i++)    {        scanf("%d %d", &probs[i].score, &useless);        probs[i].wrong = 0;        probs[i].answer = readanswer();    }    /* read every student's answer */    for(int i = 0; i < N; i++)    {        int score = 0;        for(int j = 0; j < M; j++)        {            /* read answer for one problem */            while(getchar() != '(');            if(readanswer() == probs[j].answer) /* If it is right */                score += probs[j].score;            else if(max < ++probs[j].wrong)     /* If most students got it wrong */                max = probs[j].wrong;            while(getchar() != ')');        }        printf("%d\n", score);    }    if(max == 0)        printf("Too simple");    else    {        printf("%d", max);        for(int i = 0; i < M; i++) if(probs[i].wrong == max)            printf(" %d", i + 1);    }    return 0;}
阅读全文
0 0
原创粉丝点击