PAT乙级-1073 多选题常见计分法 (20 分)

题目链接

解题思路参考柳婼

#include<iostream>
#include<vector>
using namespace std;

int main(){
    int n,m;
    cin>>n>>m;
    int hash[5]={1,2,4,8,16}; //选项个数不超过5个
    vector<int> fullscore(m),trueopthash(m);
    int optnum,trueoptnum;
    char opt;
    for(int i=0;i<m;i++){
        cin>>fullscore[i]>>optnum>>trueoptnum;
        for(int j=0;j<trueoptnum;j++){
            scanf(" %c",&opt);
            trueopthash[i]+=hash[opt-'a']; //根据二进制位1所在的位置判断选择了哪些选项
        }
    }
    vector<vector<int>> cnt(m,vector<int>(5));
    for(int i=0;i<n;i++){
        double score=0.0;
        for(int j=0;j<m;j++){
            int selnum,selopthash=0;
            getchar(); //吃掉回车
            scanf("(%d",&selnum);
            for(int k=0;k<selnum;k++){
                scanf(" %c",&opt);
                selopthash+=hash[opt-'a'];
            }
            scanf(")");
            int el=selopthash^trueopthash[j];//^二进制异或运算,二进制位相同对应位结果为0,不同对应位结果为1
            if(!el) score+=fullscore[j]; //选项全正确
            else{
                if((selopthash|trueopthash[j])==trueopthash[j]) //选项部分正确,注意加括号(selopthash|trueopthash[j])
                    score+=fullscore[j]*1.0/2; //|二进制或运算,只要二进制位有一个为1,则对应位结果为1
                for(int k=0;k<5;k++) //找出错误选项号
                    if(el&hash[k]) cnt[j][k]++; //&二进制与运算,二进制位都为1,对应位结果才为1
            }
        }
        printf("%.1f\n",score);
    }
    int maxcnt=0;
    for(int i=0;i<m;i++)
        for(int j=0;j<5;j++)
            if(cnt[i][j]>maxcnt) maxcnt=cnt[i][j];
    if(maxcnt==0) cout<<"Too simple"<<endl;
    else{
        for(int i=0;i<m;i++)
            for(int j=0;j<5;j++)
                if(cnt[i][j]==maxcnt) printf("%d %d-%c\n",maxcnt,i+1,j+'a');
    }
    return 0;
}

知识:位运算符的使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值