同学发来一编程题目,图:

上来就一张图,实不相瞒,前八眼硬是没看懂要干嘛,一番说明后才略懂题意:

前面的选择题也是题干,目的是给出十道题的答案,判断给出的答案是否合法(符合10道选择题的描述)。

第二问则是编写程序给出合法的答案。

思路:

问题一:把10个问题的文字描述转换为代码,然后用输入的答案与每题符合规则的答案比对,如果出现不同答案则不合法。

问题二:基于问题一的代码,10个循环暴力解决。

以上解决方案确实不需要很长的思考时间和深厚的代码功底,所以性能上肯定是最差的。期待一个更好的解决方案。

以下是python3代码:

ANS = list([])


def Q2(a5):
    if a5 == 1:
        return 3
    elif a5 == 2:
        return 4
    elif a5 == 3:
        return 1
    elif a5 == 4:
        return 2
    else:
        return 0


def Q3(a3, a6, a2, a4):
    if a3 == a6 == a2:
        return 4
    elif a3 == a6 == a4:
        return 3
    elif a3 == a2 == a4:
        return 2
    elif a6 == a2 == a4:
        return 1
    else:
        return 0


def Q4(a1, a2, a5, a6, a7, a9, a10):
    if a1 == a5:
        return 1
    elif a2 == a7:
        return 2
    elif a1 == a9:
        return 3
    elif a6 == a10:
        return 4
    else:
        return 0


def Q5(a5, a8, a4, a9, a7):
    if a5 == a8:
        return 1
    elif a5 == a4:
        return 2
    elif a5 == a9:
        return 3
    elif a5 == a7:
        return 4
    else:
        return 0


def Q6(a8, a2, a4, a1, a6, a3, a10, a5, a9):
    if a8 == a2 == a4:
        return 1
    elif a8 == a1 == a6:
        return 2
    elif a8 == a3 == a10:
        return 3
    elif a8 == a5 == a9:
        return 4
    else:
        return 0


def Q7(most_chose):
    if most_chose == 1:
        return 3
    elif most_chose == 2:
        return 2
    elif most_chose == 3:
        return 1
    elif most_chose == 4:
        return 4
    else:
        return 0


def Q8(a1, a7, a5, a2, a10):
    if (a7 + 1 != a1) or (a7 - 1 != a1):
        return 1
    elif (a5 + 1 != a1) or (a5 - 1 != a1):
        return 2
    elif (a2 + 1 != a1) or (a2 - 1 != a1):
        return 3
    elif (a10 + 1 != a1) or (a10 - 1 != a1):
        return 4
    else:
        return 0
    

def Q9(a1, a6, a5, a10, a2, a9):
    if (a1 == a6) != (a5 == a6):
        return 1
    elif (a1 == a6) != (a5 == a10):
        return 2
    elif (a1 == a6) != (a5 == a2):
        return 3
    elif (a1 == a6) != (a5 == a9):
        return 4
    else:
        return 0


def Q10(diff):
    if diff == 1:
        return 4
    elif diff == 2:
        return 2
    elif diff == 3:
        return 1
    elif diff == 4:
        return 3
    else:
        return 0


def most_chose_diff(ans):
    dic = {1: 0, 2: 0, 3: 0, 4: 0}
    for each in ans:
        dic[each] += 1
    lst = sorted(dic, reverse=True)
    lst2 = sorted(dic.values(), reverse=True)
    return lst[0], lst2[0]-lst2[3]


def validate(ans):
    mostchose, diff = most_chose_diff(ans)
    if ans[1] != Q2(ans[4]):
        return False, 2
    if ans[2] != Q3(ans[2], ans[5], ans[1], ans[3]):
        return False, 3
    if ans[3] != Q4(ans[0], ans[1], ans[4], ans[5], ans[6], ans[8], ans[9]):
        return False, 4
    if ans[4] != Q5(ans[4], ans[7], ans[3], ans[8], ans[6]):
        return False, 5
    if ans[5] != Q6(ans[7], ans[1], ans[3], ans[0], ans[5], ans[2], ans[9], ans[4], ans[8]):
        return False, 6
    if ans[6] != Q7(mostchose):
        return False, 7
    if ans[7] != Q8(ans[0], ans[6], ans[4], ans[1], ans[9]):
        return False, 8
    if ans[8] != Q9(ans[0], ans[5], ans[4], ans[9], ans[1], ans[8]):
        return False, 9
    if ans[9] != Q10(diff):
        return False, 10
    return True, 0


def get_ans():
    for a1 in range(1, 5):
        for a2 in range(1, 5):
            for a3 in range(1, 5):
                for a4 in range(1, 5):
                    for a5 in range(1, 5):
                        for a6 in range(1, 5):
                            for a7 in range(1, 5):
                                for a8 in range(1, 5):
                                    for a9 in range(1, 5):
                                        for a10 in range(1, 5):
                                            is_val, num = validate([a1, a2, a3, a4, a5, a6, a7, a8, a9, a10])
                                            if is_val:
                                                return [a1, a2, a3, a4, a5, a6, a7, a8, a9, a10]


def main():
    print("Correct answer is " + str(get_ans()))
    ANS = input().split(" ")
    ANS = [int(x) for x in ANS]
    isval, number = validate(ANS)
    if isval:
        print("OK")
    else:
        print("Wrong " + str(number))


if __name__ == "__main__":
    main()

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据