ACjudge0x1
在打算法题的时候,我通常使用wsl2+vscode的组合(感觉默认cpp提示太烂了,vs过于臃肿),当我们完成一道题目时,通常要使用g++编译,然后用cv大法查看是否ac。可是,在debug时,重复的动作过于痛苦,且难以区分输入输出。因此可以使用linux命令来解决这一问题。
1
| g++ {path} && ./a.out < input.txt > output.txt && diff output.txt answer.txt -y
|
我们当然可以写入shell脚本,并加上初始化功能(- i)
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
|
RED='\033[31m' GREEN='\033[32m' YELLOW_BOLD='\033[1;33m' NC='\033[0m'
if [ -z "$1" ]; then echo "用法: $0 <文件夹名或题目名>" exit 1 fi
TARGET=$1
init_folder() { local folder=$1 echo -e "${YELLOW_BOLD}正在为目录 '$folder' 执行初始化...${NC}" mkdir -p "$folder" touch "$folder/input.txt" touch "$folder/answer.txt" if [ ! -f "$folder/main.cpp" ]; then echo -e "#include <iostream>\nusing namespace std;\n\nint main() {\n return 0;\n}" > "$folder/main.cpp" fi echo -e "${GREEN}初始化完成!已创建 input.txt, answer.txt 和 main.cpp${NC}" }
judge_folder() { local folder=$1 cd "$folder" || exit
local cpp_file=$(ls *.cpp 2>/dev/null | head -n 1)
if [ -z "$cpp_file" ]; then echo -e "${RED}错误:在 $folder 中未找到 .cpp 文件${NC}" return fi
echo -e "${YELLOW_BOLD}正在评测目录: $folder (文件: $cpp_file)${NC}"
if g++ "$cpp_file" -Wall -o a.out; then echo "编译成功..." else echo -e "${RED}编译失败${NC}" return fi
if [ ! -f "input.txt" ]; then touch input.txt; fi if [ ! -f "answer.txt" ]; then touch answer.txt; fi
./a.out < input.txt > output.txt
if diff output.txt answer.txt -y; then echo -e "${GREEN}AC !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!${NC}" else echo -e "${RED}WA (解答错误),请检查输出差异:${NC}" fi }
if [ -d "$TARGET" ]; then judge_folder "$TARGET" else init_folder "$TARGET" fi
|
然后将python文件移动到系统脚本目录
1 2
| chmod +x ACjudge.sh mv ./ACjudge.sh /usr/local/bin/ACjudge.sh
|
当然,脚本还有许多可以添加的功能,如创建文件时自动添加模板……如果可以的话,还可以加个python爬虫(针对特定的平台,如洛谷,codeforce),当我们创建对应的文件时,自动爬取响应的输入输出。
vscode已经有许多插件实现这一效果,这里主要针对是平时练习时插件不太好使或者使用小众比赛的情况下。
shell
shell超简洁版本
```sh
!/bin/bash
x=$1
touch “${x}_input.txt” “${x}_output.txt” “${x}_answer.txt” “${x}.cpp”
g++ “${x}.cpp” -o “${x}.out” && \
./“${x}.out” < “${x}_input.txt” > “${x}_output.txt” && \
diff -y “${x}_output.txt” “${x}_answer.txt” 2>/dev/null