【JAVA】 PAT 乙级 1073 多选题常见计分法 (踩点通过)

【JAVA】 PAT 乙级 1073 多选题常见计分法 (测试点4超时)


题目链接

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

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

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

输入样例 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)

输出样例 1:

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

输入样例 2:

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

输出样例 2:

5.0
5.0
Too simple

这道题和1058 选择题有点类似,部分代码可以直接使用。
这道题的难点在于要理解题意,题目要求的是输出错误最多的选项的信息,以及少选的也不能漏下。比如说样例一,第一题错误了一次,错误选项的次数b一次d一次,第二题错了两次,错误选项d一次e两次,第三题错了三次,错误选项a两次b两次,第四题没人选错。
第三题错得最多,但是样例的输出错误次数是2而不是3,所以是统计所有所有题目中所有选项中最多的选项信息。

相比于1058的代码,我在题目的类tm里增加了一个整数数组成员变量x,用于储存每道题错误的次数,和记录最大次数的下标imax。

检查学生答案的方法:

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) {//错选直接未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];
}

sum记录所有题目的总分,其他部分与1058代码类似

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);
}

count记录没有满分的学生数量。其他部分与1058代码类似

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++;
	}
}

如果count为0,输出“Too simple”,否则首先对t排序,获取排序后第一题的最大错误选项次数max。遍历t,获取当前题目的错误最多的选项次数tmax,与max对比,不相等退出循环结束程序,相等则遍历tem的选项次数数组x,等于tmax的时候输出

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();

虽然过了,但是是踩点过的,如果读者在PAT上最后一个样例超时了,可以多提交几次试试
题目要求400ms
题目要求400ms内,这道题我399ms(和1058一样hhhhh)

完整代码(测试点4超时)

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];
		}
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值