1. 程式人生 > 其它 >pat乙級 1073 多選題常見計分法 與1058 選擇題

pat乙級 1073 多選題常見計分法 與1058 選擇題

兩道題很相似並且都沒有什麼難度,唯一的問題就是要處理的資料比較多,用python解要處理好列表的包含關係
首先1058

    N, M=map(int,input().split())
    nums = []
    ans = []
    score = []
    for i in range(M):
        ls = list(input().split())
        scores = int(ls[0])
        right = int(ls[2])
        choice = ls[3:]#讀入分數和正確答案
        ans.append([scores, right, choice])#記錄每個分數和正確答案
    error = [0 for i in range(M)]#用於記錄每道題錯誤的次數
    score = [0 for i in range(N)] #用於記錄每位學生的分數
    for i in range(N):
        ls = input()
        ls = ls.replace('(', '')
        ls = ls.split(')')#處理括號
        for j in range(M):
            l = ls[j].strip().split()
            num = int(l[0])
            choice = ans[j]
            anss = l[1:]
            if anss == choice[2]:#每道題都判斷,如果對了就加分,錯了就記錄錯誤次數加一
                score[i] += choice[0]
            else:
                error[j] += 1
    for a in score:#輸出分數
        print(a)
    max = max(error)
    if max == 0 :
        print("Too simple")
    else :#輸出錯誤最多的題目
        result = []
        for index, errorlist in enumerate(error, 1):
            if errorlist == max:
                result.append(str(index))
        result = [str(max)] + result
        print(' '.join(result))

然後1073,這個比上一題要複雜一些,但是整體思路和上一題一樣,在上一題基礎上加一個記錄每個選項錯誤的次數

    N, M=map(int,input().split())
    nums = []
    ans = []
    score = []
    for i in range(M):
        ls = list(input().split())
        scores = int(ls[0])
        right = int(ls[2])
        choice = ls[3:]
        wrong = {}
        for j in ['a', 'b', 'c', 'd', 'e']:#每一道題目都記錄每個選項錯誤的次數,對的沒選和錯的選了都算錯
            wrong[j] = 0
        ans.append([scores, right, choice, wrong])
    error = [0 for i in range(M)]  
    score = [0 for i in range(N)] 
    for i in range(N):
        ls = input()
        ls = ls.replace('(', '')
        ls = ls.split(')')
        for j in range(M):
            l = ls[j].strip().split()
            num = int(l[0])
            choice = ans[j]
            anss = l[1:]
            if set(anss) <= set(choice[2]):
                if anss == choice[2]:
                    score[i] += choice[0]
                else :
                    score[i] += choice[0] / 2#注意這個不算總的錯誤次數
                    for m in choice[2]:#對了沒選
                        if m not in anss:
                            ans[j][3][m] +=1
            else:
                for m in choice[2]:#對了沒選
                    if m not in anss:
                        ans[j][3][m] +=1
                for n in anss:#錯的選了
                    if n not in choice[2]:
                        ans[j][3][n] +=1            
                error[j] += 1
    for a in score:
        print("{:.1f}".format(a))
    max = max(error)#這裡的error只是題目的錯誤次數,如果為零就輸出Too Simple
    if max == 0 :
        print("Too simple")
    else :#如果不為零就不要按照這個記為最大值,需要在每個選項中重新尋找最大值,
        #因為如果還是計算每道題目最大值的話,可能一道題目的選項錯誤比較分散,而這1073只需要輸出單個選項錯誤
        #類似一道題目錯了三次,但是是abc選項各一次,在最後輸出的時候找不到等於三的錯誤選項導致沒有輸出,所以這裡要再找一次錯誤次數最多的選項
        max = 0
        for m in ans:
            for n in m[3]:
                if m[3][n] > max :
                    max = m[3][n]
        a = 0
        for m in ans:
            a += 1
            for n in m[3]:
                if m[3][n] == max :
                    print("{} {}-{}".format(max, a, n))