1058 选择题(C语言)

本文介绍了一个使用位运算处理选择题答案的C语言程序设计思路。该程序通过结构体存储题目信息,利用位运算将字母答案转换为整数,以便进行快速比较。程序包括读取答案的函数,并统计了每个问题被答错的次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

设计思路:
  1. 难点在于处理好读入,数据用结构体存一下
  2. readanswer() 读取答案,读取答案过程利用位运算让其转换为 int 型,便于计算比较。这样的话“正确选型个数”实际上用不到,当废弃值处理即可
编译器:C (gcc)
#include <stdio.h>

struct question {
        int score;
        int answer;
        int wrong;
};

int readanswer();

int main()
{
        struct question questions[100];
        int n, m;
        int tmp, score, max = 0;
        int i, j;
        char ch;

        scanf("%d %d", &n, &m);
        for (i = 0; i < m; i++) {
                scanf("%d %d", &questions[i].score, &tmp);
                questions[i].answer = readanswer();
                questions[i].wrong = 0;
        }
        for (i = 0; i < n; i++) {
                score = 0;
                for (j = 0; j < m; j++) {
                        while ((ch = getchar()) != '(')
                                continue;
                        if (readanswer() == questions[j].answer)
                                score += questions[j].score;
                        else if (++questions[j].wrong > max)
                                max = questions[j].wrong;
                }
                printf("%d\n", score);
        }

        if (max == 0) {
                printf("Too simple\n");
        } else {
                printf("%d", max);
                for (i = 0; i < m; i++)
                        if (questions[i].wrong == max)
                                printf(" %d", i + 1);
        }

        return 0;
}

int readanswer()
{
        int answer = 0;
        char ch;

        while ((ch = getchar()) && ch != '\n' && ch != ')')
                if (islower(ch))
                        answer |= 1 << (ch - 'a');

        return answer;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值