1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| import json as j import re as r import functools as fcts pid = input("请输入题号:") with open("./problem (origin).json", "r") as f: for k in f.readlines(): if k.find("{\"pid\":\"" + pid + "\",") != -1: print("找到题目!") print("正在解析 JSON…") info = j.JSONDecoder().decode(k) print("正在创建文件…") with open("./" + pid + "_markdown.md", "w", encoding='utf-8') as o: print("写入标题…") print("# " + pid + " " + info['title'], end = "\n\n", file = o) print("写入题目背景…") if info['background'] == None or info['background'].replace(' ', '').replace('\t', '').replace('\n', '') == "": print("题目背景为空。") else: print("题目背景非空。") print("## 题目背景", end = "\n\n", file = o) print(info['background'], file = o) print("写入题目描述…") print("\n## 题目描述\n", file = o) print(info['description'], file = o) print("写入输入输出格式…") print("\n## 输入格式\n", file = o) print(info['inputFormat'], file = o) print("\n## 输出格式\n", file = o) print(info['outputFormat'], file = o) print("写入样例…") print("\n## 输入输出样例\n", file = o) for i in range(len(info['samples'])): print("写入第 " + str(i + 1) + " 个样例…") print("### 样例 " + str(i + 1), end = "\n\n", file = o) print("样例 " + str(i+1) + " 输入:", end = "\n\n", file = o) print("```plaintext", file = o) print("写入样例输入…") print(info['samples'][i][0].rstrip('\n'), file = o) print("```", file = o) print("样例 " + str(i+1) + " 输出:", file = o) print("```plaintext", file = o) print("写入样例输出") print(info['samples'][i][1].rstrip('\n'), file = o) print("```", file = o) if len(info['samples']) == 0: print("没有找到样例。") print("无。", file = o) print("写入说明提示…") print("## 说明/提示", file = o) print(info['hint'], file = o) print("写入附加信息(包括时间限制、空间限制、题目难度与标签)…") print("## 附加信息", file = o) if len(info['limits']['time']) == 0: print('没有找到测试点。') print("无测试点。", file = o) else: gt = lambda x: "$" + (f"{x}" + "\\mathrm{ms}" if x <= 999 else f"{x/1000}" + "\\mathrm{s}") + "$" gm = lambda x: "$" + (f"{x}" + "\\mathrm{KB}" if x <= 1023 else ("{:.2f}".format(x/1024) + "\\mathrm{MB}" + (f"({x}" + "\\mathrm{KB}" + ")" if float("{:.2f}".format(x/1024)) != x/1024 else ""))) + "$" print("写入最大/最小时间/空间限制…") tmin = min(info['limits']['time']) tmax = max(info['limits']['time']) mmin = min(info['limits']['memory']) mmax = max(info['limits']['memory']) if tmin == tmax: print(f"时间限制:{gt(tmin)} ", file = o) else: print(f"时间限制:{gt(tmin)} \\~ {gt(tmax)} ", file = o) if mmin == mmax: print(f"空间限制:{gm(mmin)}", end = "\n\n", file = o) else: print(f"空间限制:{gm(mmin)} \\~ {gm(mmax)}", end = "\n\n", file = o) print("写入时空限制表格…") print("### 时空限制", file = o) print('写入表头…') print(end = '| 限制 |', file = o) for i in range(len(info['limits']['time'])): print(end = ' 测试点 $' + str(i + 1) + '$ |', file = o) print('写入对齐信息…') print('\n' + '|:-----' * len(info['limits']['time']) + '|:-----|', file = o) print('写入时间限制…') print(end = "| 时间限制 |", file = o) for i in info['limits']['time']: print(end = f" {gt(i)} |", file = o) print('写入空间限制…') print(end = "\n| 空间限制 |", file = o) for i in info['limits']['memory']: print(end = f" {gm(i)} |", file = o) print(file = o) print('写入难度与标签…') print("### 难度与标签", file = o) print("难度:" + ["暂未评定","入门","普及-","普及/提高-","普及+/提高","提高+/省选-","省选/NOI-","NOI/NOI+/CTSC"][info['difficulty']], end = "。\n\n", file = o) print("标签:" + (fcts.reduce(lambda x,y: x+"、"+y, info['tags']) if info['tags'] != [] else "无"), end = "。\n", file = o) break else: print("无法找到题目!") exit(1)
|