Common scoring method for Java 1073 multiple choice questions

Title Content:

Correcting multiple-choice questions is a troublesome thing. There are many different scoring methods. One of the most common scoring methods is: if the examinee selects some correct options and does not select any wrong options, he will get a 50% score; If the candidate chooses any of the wrong options, he will not be able to score. Please write a program to help the teacher correct multiple-choice questions and point out which of the questions has the most wrong choices.

Input format:

Input two positive integers N (≤ 1000) and M (≤ 100) are given in the first line, which are the number of students and the number of multiple topics respectively. Then, in line M, the full score of a question (a positive integer not exceeding 5), the number of options (a positive integer not less than 2 and not more than 5), the number of correct options (a positive integer not exceeding the number of options), and all correct options are given in sequence in each line. Note that the options of each question are arranged in order starting from the English letter A. Items are separated by 1 space. In the last N lines, each line gives a student's answer, and the answer format of each question is   (number of selected options, option 1...) are given in the order of topics. Note: the questions ensure that students' answers are legal, that is, there is no case where the number of selected options exceeds the actual number of options.

Output format:

The scores of each student are given in the order of input. Each score occupies one line and outputs one decimal place. Finally, the information of the most wrong topic option is output in the format of: number of errors, topic number (topics are numbered from 1 in the input order) - option number. If there is juxtaposition, one option for each line will be output in ascending order of topic number; If they are juxtaposed again, they will be output in ascending order of option number. There must be no extra spaces at the beginning and end of the line. If all the questions are correct, output them on the last line   Too simple.

Input example 1:

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)

No blank lines at the end

Output example 1:

3.5
6.0
2.5
2 2-e
2 3-a
2 3-b

No blank lines at the end

Input example 2:

2 2 
3 4 2 a c
2 5 1 b
(2 a c) (1 b)
(2 a c) (1 b)

No blank lines at the end

Output example 2:

5.0
5.0
Too simple

No blank lines at the end

Java code implementation (no AC, test point 4 timeout):

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
		StreamTokenizer in = new StreamTokenizer(bf);
		in.wordChars('(', '(');
		in.wordChars(')', ')');
		in.nextToken();
		int N = (int) in.nval;
		in.nextToken();
		int M = (int) in.nval;
		tm[] t = new tm[M];
		double sum = 0;
		for (int i = 0; i < M; i++) {
			in.nextToken();
			int fs = (int) in.nval;
			in.nextToken();
			int gs = (int) in.nval;
			in.nextToken();
			int zqgs = (int) in.nval;
			int[] ans = new int[5];
			sum += fs;
			for (int j = 0; j < zqgs; j++) {
				in.nextToken();
				ans[in.sval.charAt(0) - 'a'] = 1;

			}
			t[i] = new tm(i + 1, fs, gs, zqgs, ans);
		}
		int count = 0;
		for (int i = 0; i < N; i++) {
			double fs = 0;
			for (int j = 0; j < M; j++) {
				in.nextToken();
				int sgs = in.sval.charAt(1) - '0';
				int[] sans = new int[5];
				for (int k = 0; k < sgs; k++) {
					in.nextToken();
					sans[in.sval.charAt(0) - 'a'] = 1;
				}
				fs += t[j].check(sgs, sans);
			}
			out.printf("%.1f\n", fs);
			if (fs != sum) {
				count++;
			}
		}
		if (count == 0) {
			out.println("Too simple");
		} else {
			Arrays.sort(t);
			int max = t[0].x[t[0].imax];
			for (tm tem : t) {
				int tmax = tem.x[tem.imax];
				if (tmax == max) {
					for (int i = 0; i < tem.gs; i++) {
						if (tem.x[i] == tmax) {
							out.println(max + " " + tem.bh + "-" + (char) (i + 'a'));
						}
					}
				} else {
					break;
				}
			}
		}
		out.flush();
	}

	static class tm implements Comparable<tm> {
		int bh;
		double fs;
		int gs;
		int zqgs;
		int[] ans;
		int imax = 0;
		int[] x;

		tm(int b, int f, int g, int zq, int[] a) {
			bh = b;
			fs = f;
			gs = g;
			zqgs = zq;
			ans = a;
			x = new int[g];
		}

		double check(int sgs, int[] sans) {
			double F = fs;
			for (int i = 0; i < gs; i++) {
				if (ans[i] != sans[i]) {
          x[i]++;
					if (ans[i] == 1) {
						if (F != 0) {
							F = fs / 2;
						}
					} else if (ans[i] == 0) {
						F = 0;	
					}
					if (x[imax] < x[i]) {
						imax = i;
					}
				}
			}
			return F;
		}

		@Override
		public int compareTo(tm o) {
			if (o.x[o.imax] == this.x[this.imax]) {
				return this.bh - o.bh;
			}
			return o.x[o.imax] - this.x[this.imax];
		}
	}
}

C + + code implementation (AC):

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
struct choo//Topic option information
{
    int full;
    int chnum;
    int an;
    string ch;
};
int main()
{
    int n,m,i,j,x,k,f,wr=0;
    char co;
    float s;
    cin>>n>>m;
    int wrong[m][5]={0};
    choo choo[m];
    for(i=0;i<m;i++)//option
    {
        cin>>choo[i].full>>choo[i].chnum>>choo[i].an;
        for(j=0;j<choo[i].an;j++)
        cin>>co,choo[i].ch+=co;
    }
    while(n--)//Traverse students
    {
        s=0;//Results of each student
        for(i=0;i<m;i++)
    {
        scanf(" ");//Easy input
        string ans;
        scanf("(%d",&x);
        for(j=0;j<x;j++)
            scanf(" %c",&co),ans+=co;//Spaces must be added
        scanf(")");
        if(ans==choo[i].ch)s+=choo[i].full;
        else
        {
            f=0;
            for(k=0;k<ans.length();k++)//Wrong question
        {
            choo[i].ch.find(ans[k]);
            if(choo[i].ch.find(ans[k])==-1)
            {
                wrong[i][ans[k]-'a']++;
                f=1;
                if(wrong[i][ans[k]-'a']>wr)wr=wrong[i][ans[k]-'a'];
            }
        }
        for(k=0;k<choo[i].ch.length();k++)//miss exam questions
        {
            if(ans.find(choo[i].ch[k])==-1)
            {
                wrong[i][choo[i].ch[k]-'a']++;
                if(wrong[i][choo[i].ch[k]-'a']>wr)wr=wrong[i][choo[i].ch[k]-'a'];
            }
        }
        if(f==0)s+=choo[i].full/2.0;
        }
    }
    printf("%.1f\n",s);
    }
    if(wr==0){cout<<"Too simple";return 0;}
    for(i=0;i<m;i++)
    {
        for(j=0;j<5;j++)
        {
            if(wr==wrong[i][j])
                printf("%d %d-%c\n",wr,i+1,'a'+j);
        }
    }
    return 0;
}

Tags: Java PAT

Posted on Wed, 17 Nov 2021 10:51:56 -0500 by amesowe