B 1058 选择题(对scanf(“ “)、scanf(“(%d“),&k)、scanf(“)“)的应用)

    技术2026-06-13  12

    题目

    思路

    这题考的其实不难,关键点是学生答案的输入用()来分隔每一道题。用cin就只能用string保存,再遍历判断,非常麻烦。 说实话,我非常不喜欢用scanf,直到这题教我做人……scanf对于要求格式的输入,真香!

    代码参考自秦神

    AC代码

    #include<bits/stdc++.h> using namespace std; struct test{ int score; int ans; int right; string rans; }; int main(){ int n,m; cin>>n>>m; test a[m]; for(int i=0;i<m;i++){ scanf("%d %d %d",&a[i].score,&a[i].ans,&a[i].right); char temp; for(int j=0;j<a[i].right;j++){ cin>>temp; a[i].rans +=temp; } } int wrong[m] = {0}; //存放每题错误次数 for(int i=0;i<n;i++){ getchar(); //每行第一个直接输入"(",这里必须吸收掉一个换行符 int sco = 0; for(int j=0;j<m;j++){ if(j != 0) scanf(" "); string str; int k; char s; scanf("(%d", &k); //选项个数 for(int q = 0; q < k; q ++){ scanf(" %c", &s); str += s; //str存放学生给出的答案集合 } scanf(")"); if(str == a[j].rans) sco += a[j].score; //判断集合str与正确答案集合是否相同,相同则得分 else wrong[j] ++; //否则记录当前题为错误 } cout<<sco<<endl; } int max = -1; for(int i = 0; i < m; i ++){ //遍历查询最多错误次数 if(max < wrong[i]) max = wrong[i]; } if(max == 0) cout << "Too simple"; else{ cout << max; for(int i = 0; i < m; i ++){ if(max == wrong[i]) cout << " " << i+1; } } return 0; }
    Processed: 0.008, SQL: 9