对拍Linux实现

· · 科技·工程

准备

- 正确代码 / 待拍代码 各一份(apple_bl.cpp / apple.cpp)

- 数据生成器(make.cpp)

使用 C++ 生成

- 对拍脚本(DP.sh)

解释如下

g++ apple_bl.cpp -o apple_bl
g++ apple.cpp -o apple
g++ make.cpp -o make

# 编译生成可执行程序

while true 
do
    ./make > 1.in
    ./apple_bl < 1.in > right.out
    ./apple < 1.in > wrong.out

    # 生成数据并且产生输出

    diff -Z right.out wrong.out
    if [ $? -ne 0 ]; then
        echo "" # 换行用
        cat 1.in
        echo ""
        break
    fi

    # 比较,不同则输出产生不同的输入输出
done

$?是上一个语句产生的返回值(上面就是diff函数返回的,不同:1 / 相同:0)

-ne相当于!=

- 获取可执行权限

chmod +x DP.sh

- 运行

在终端(Linux 终端或 VScode终端)输入./DP.sh运行。

- 效果

或者直接在1.inright.out / wrong.out查看。