PAT Basic 1073. 多选题常见计分法

发布时间 2023-04-06 22:09:32作者: 十豆加日月

PAT Basic 1073. 多选题常见计分法

1. 题目描述:

批改多选题是比较麻烦的事情,有很多不同的计分方法。有一种最常见的计分方法是:如果考生选择了部分正确选项,并且没有选择任何错误选项,则得到 50% 分数;如果考生选择了任何一个错误的选项,则不能得分。本题就请你写个程序帮助老师批改多选题,并且指出哪道题的哪个选项错的人最多。

2. 输入格式:

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

3. 输出格式:

按照输入的顺序给出每个学生的得分,每个分数占一行,输出小数点后 1 位。最后输出错得最多的题目选项的信息,格式为:错误次数 题目编号(题目按照输入的顺序从1开始编号)-选项号。如果有并列,则每行一个选项,按题目编号递增顺序输出;再并列则按选项号递增顺序输出。行首尾不得有多余空格。如果所有题目都没有人错,则在最后一行输出 Too simple

4. 输入样例:

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) (3 b d e) (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) (1 c) (4 a b c d)
2 2 
3 4 2 a c
2 5 1 b
(2 a c) (1 b)
(2 a c) (1 b)

5. 输出样例:

3.5
6.0
2.5
2 2-e
2 3-a
2 3-b
5.0
5.0
Too simple

6. 性能要求:

Code Size Limit
16 KB
Time Limit
400 ms
Memory Limit
64 MB

思路:

这道题搞了一晚上。。。这里定义一个结构体Question用于存储每道题目相关的信息,其中points存储分值,answer存储正确答案,因为选项个数最多为5,数组大小定义为5,wrongCount存储错误次数(一开始理解错了,这个元素其实用不到,懒得删了。。。),wrongOption存储每个选项的错误次数。

这里关键就是每个学生的答题情况的输入,需要用getchar()处理几处细节,确保正确输入。第一次提交testpoint3,4报wrong answer,检查了逻辑感觉没问题。网上查了查才发现题目是要求输出错得最多的"题目选项"的信息,我一开始还以为是错的最多的“题目”选项的信息。。。另外有个点就是库函数calloc的使用,第一个形参是元素个数,第二个形参是每个元素占的字节大小。一直用的malloc比较多,这里一开始传参错了,但是程序能正常运行,因为分配的内存大小是正确的,这一点还是比较有意思。

My Code:

#include <stdio.h>
#include <stdlib.h> // calloc header

typedef struct question
{
    int points;
    int answer[5]; // 11111 represent correct answer is abcde
    int wrongCount;
    int wrongOption[5];
} Question;

int judgeQuestion(const int *answer, const int *solution, int *wrongOption);

// first submit testpoint3, 4 wrong answer, for I misunderstand the question meaning.
int main(void)
{
    int stuCount=0, quesCount=0;
    int i=0, j=0, k=0; // iterator
    int optCount=0, correctCount=0;
    Question * pQues = NULL;
    double tempGrade = 0.0;
    char tempCh = '\0';
    int tempAnswer[5] = {0};
    int maxWrongCount = 0;
    //int maxWrongOption = 0;
    
    scanf("%d%d", &stuCount, &quesCount);
    //void *calloc(size_t nitems, size_t size)
    pQues = (Question *)calloc(quesCount, sizeof(Question));
    //pQues = (Question *)calloc(sizeof(Question), quesCount);
    for(i=0; i<quesCount; ++i)
    {
        scanf("%d%d%d", &pQues[i].points, &optCount, &correctCount);
        for(j=0; j<correctCount; ++j)
        {
            scanf(" %c", &tempCh);
            pQues[i].answer[tempCh-'a'] = 1; // set correct answer
        }
    }
    
    getchar(); // consume the '\n'
    for(i=0; i<stuCount; ++i) // traverse every student
    {
        tempGrade = 0.0; // reset tempGrade
        for(j=0; j<quesCount; ++j) // traverse every question
        {
            for(k=0; k<5; ++k) { tempAnswer[k]= 0; } // reset answer record
            scanf("(%d", &optCount);
            for(k=0; k<optCount; ++k) // traverse every option
            {
                scanf(" %c", &tempCh);
//                 printf("%c ", tempCh);
                tempAnswer[tempCh-'a'] = 1;
            }
//             printf("\n");
            getchar(); // consume ')'
            getchar(); // consume ' ' or '\n'
            
            switch(judgeQuestion(pQues[j].answer, tempAnswer, pQues[j].wrongOption))
            {
                case 1: // have wrong answer
                    ++pQues[j].wrongCount;
                    break;
                case 2: // half-correct
                    //++pQues[j].wrongCount; // from test example, half-correct doesn't belong to wrongCount.
                    tempGrade += 0.5*pQues[j].points;
                    break;
                case 3: // full-correct
                    tempGrade += pQues[j].points;
                    break;
                default:
                    break;
            }
        }
        printf("%.1lf\n", tempGrade);
    }
    
    for(i=0; i<quesCount; ++i)
    {
        for(j=0; j<5; ++j)
        {
            if(maxWrongCount < pQues[i].wrongOption[j])
            {
                maxWrongCount = pQues[i].wrongOption[j];
            }
        }
    }
    
    if(!maxWrongCount) // full-correct
    {
        printf("Too simple\n");
    }
    else
    {
        for(i=0; i<quesCount; ++i)
        {
            for(j=0; j<5; ++j)
            {
                if(maxWrongCount == pQues[i].wrongOption[j])
                {
                    printf("%d %d-%c\n", maxWrongCount, i+1, j+'a');
                }
            }
        }
    }
    
    
//     for(i=0; i<quesCount; ++i)
//     {
//         if(pQues[i].wrongCount > maxWrongCount)
//         {
//             maxWrongCount = pQues[i].wrongCount;
//         }
//     }
    
//     if(!maxWrongCount) // does'n have wrong answer
//     {
//         printf("Too simple\n");
//     }
//     else
//     {
//         for(i=0; i<quesCount; ++i)
//         {
//             if(pQues[i].wrongCount == maxWrongCount)
//             {
//                 maxWrongOption = 0;
//                 for(j=0; j<5; ++j) // find max wrong option
//                 {
//                     if(pQues[i].wrongOption[j] > maxWrongOption)
//                     {
//                         maxWrongOption = pQues[i].wrongOption[j];
//                     }
//                 }
                
//                 for(j=0; j<5; ++j)
//                 {
//                     if(pQues[i].wrongOption[j] == maxWrongOption)
//                     {
//                         printf("%d %d-%c\n", maxWrongCount, i+1, j+'a');
//                     }
//                 }
//             }
//         }
//     }
    
    free(pQues);
    return 0;
}

// return 1: have wrong answer, 2: half-correct, 3: full-correct
int judgeQuestion(const int *answer, const int *solution, int *wrongOption)
{
    int i=0;
    int stateFlag = 0;
    
    for(i=0; i<5; ++i)
    {
        if(answer[i] != solution[i]) // doesn't full-correct
        {
            if(!answer[i]) // don't have i option, then solution must be 1
            {
                ++wrongOption[i];
                stateFlag = 1;
            }
            else // have i option, then solution must be 0
            {
                ++wrongOption[i];
                if(!stateFlag) // if does'n have wrong answer, then set stateFlag == 2
                {
                    stateFlag = 2;
                }
            }
        }
    }
    if(!stateFlag) // full-correct
    {
        stateFlag = 3;
    }
    
    return stateFlag;
//     for(i=0; i<5; ++i) // test input is correct
//     {
//         printf("%d ", solution[i]);
//     }
//     printf("\n");
}