我的PAT系列文章更新重心已移至Github,歡迎來看PAT題解的小伙伴請(qǐng)到Github Pages瀏覽最新內(nèi)容。此處文章目前已更新至與Github Pages同步。歡迎star我的repo。
題目
批改多選題是比較麻煩的事情进泼,本題就請(qǐng)你寫個(gè)程序幫助老師批改多選題,并且指出哪道題錯(cuò)的人最多纤虽。
輸入格式:
輸入在第一行給出兩個(gè)正整數(shù) N( 1000)和 M( 100)乳绕,分別是學(xué)生人數(shù)和多選題的個(gè)數(shù)。隨后 M
行逼纸,每行順次給出一道題的滿分值(不超過 5 的正整數(shù))洋措、選項(xiàng)個(gè)數(shù)(不少于 2 且不超過 5
的正整數(shù))、正確選項(xiàng)個(gè)數(shù)(不超過選項(xiàng)個(gè)數(shù)的正整數(shù))杰刽、所有正確選項(xiàng)菠发。注意每題的選項(xiàng)從小寫英文字母 a 開始順次排列。各項(xiàng)間以 1 個(gè)空格分隔贺嫂。最后 N
行滓鸠,每行給出一個(gè)學(xué)生的答題情況,其每題答案格式為 (選中的選項(xiàng)個(gè)數(shù) 選項(xiàng)1 ……)
第喳,按題目順序給出糜俗。注意:題目保證學(xué)生的答題情況是合法的,即不存在選中的選項(xiàng)數(shù)超過實(shí)際選項(xiàng)數(shù)的情況曲饱。
輸出格式:
按照輸入的順序給出每個(gè)學(xué)生的得分吩跋,每個(gè)分?jǐn)?shù)占一行。注意判題時(shí)只有選擇全部正確才能得到該題的分?jǐn)?shù)渔工。最后一行輸出錯(cuò)得最多的題目的錯(cuò)誤次數(shù)和編號(hào)(題目按照輸入的順序從
1 開始編號(hào))。如果有并列桥温,則按編號(hào)遞增順序輸出引矩。數(shù)字間用空格分隔,行首尾不得有多余空格。如果所有題目都沒有人錯(cuò)旺韭,則在最后一行輸出 Too simple
氛谜。
輸入樣例:
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) (2 b d) (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) (2 b c) (4 a b c d)
輸出樣例:
3
6
5
2 2 3 4
思路
這道題稍微復(fù)雜一些,但是拆分成一個(gè)個(gè)要點(diǎn)区端,每一點(diǎn)又是很簡(jiǎn)單的值漫。
- 使用結(jié)構(gòu)數(shù)組存儲(chǔ)每一道題的分?jǐn)?shù)、選項(xiàng)數(shù)织盼、正確選項(xiàng)數(shù)杨何、正確選項(xiàng)和學(xué)生答錯(cuò)數(shù)量。
當(dāng)然如何存儲(chǔ)這些量每個(gè)人不盡相同沥邻,下面是我的方法:- 在一個(gè)整型變量中按位存儲(chǔ)一道題的正確選項(xiàng)危虱,如答案是a b d e,那么這個(gè)量就是二進(jìn)制00011011(從低向高存儲(chǔ))唐全,
- 這樣直接比較整型的大小就知道答案是否一致埃跷,并且相當(dāng)于選項(xiàng)數(shù)、正確選項(xiàng)數(shù)邮利、正確選項(xiàng)三項(xiàng)信息綜合在了一起弥雹。
- 讀取
- 讀取學(xué)生答案的時(shí)候,要注意如何處理括號(hào)延届,
- 發(fā)現(xiàn)C語言讀取空格分隔的字母還挺麻煩剪勿。我是用的
while((c = getchar()) != ' ');
讀到非空白字符。
寫出來代碼好多祷愉,是乙級(jí)里代碼最多的幾題了╮(╯▽╰)╭(難道是我的問題窗宦?)
代碼
最新代碼@github,歡迎交流
#include <stdio.h>
typedef struct prob{
int score;
int answer; /* bitwise storage for at most 5 options */
int wrong;
} Prob;
/* read 'count option1 ...' format */
int readanswer()
{
char c;
int count, answer = 0;
scanf("%d", &count);
for(int k = 0; k < count; k++)
{
while((c = getchar()) == ' ') ;
answer |= 1 << (c - 'a');
}
return answer;
}
int main()
{
int N, M, max = 0, useless;
Prob probs[100];
/* read the answers for each problem */
scanf("%d %d", &N, &M);
for(int i = 0; i < M; i++)
{
scanf("%d %d", &probs[i].score, &useless);
probs[i].wrong = 0;
probs[i].answer = readanswer();
}
/* read every student's answer */
for(int i = 0; i < N; i++)
{
int score = 0;
for(int j = 0; j < M; j++)
{
/* read answer for one problem */
while(getchar() != '(');
if(readanswer() == probs[j].answer) /* If it is right */
score += probs[j].score;
else if(max < ++probs[j].wrong) /* If most students got it wrong */
max = probs[j].wrong;
while(getchar() != ')');
}
printf("%d\n", score);
}
if(max == 0)
printf("Too simple");
else
{
printf("%d", max);
for(int i = 0; i < M; i++)
if(probs[i].wrong == max)
printf(" %d", i + 1);
}
return 0;
}