- 定义函数
run() {
g++ $1.cpp -o $1 -O2 -Wall -std=c++14;
if [ $? == 0 ]; then
time ./$1;
fi
}在终端输入以上内容即可。
之后可以使用 run + 文件名运行程序。
比用 C++ 实现方便很多。
~/.bashrc
这个文件是每次新建终端会执行一遍的代码。
在命令行用一个编辑器打开(比如 VSCode 就是 code ~/.bashrc)。
加入上述函数代码。那么每次新建终端就都可以用了。
加入 ulimit -s 512000,即可在每次新建终端的时候自动开栈。
- 其他函数示例
runf() {
g++ $1.cpp -o $1 -O2 -Wall -std=c++14 -fsanitize=address,undefined;
if [ $? == 0 ]; then
time ./$1;
fi
}使用 -fsanitize。
ckd() {
data=$1;
std=$2;
mine=$3;
file=$4;
for ((i=1;;i++)) do
./$data 2> /dev/null; if [ $? != 0 ]; then echo "generator failed on test $i."; break; fi
./$std 2> /dev/null; if [ $? != 0 ]; then echo "std failed on test $i."; break; fi
./$mine 2> /dev/null; if [ $? != 0 ]; then echo "runtime error on test $i."; break; fi
diff $file.out $file.ans; if [ $? != 0 ]; then echo "wrong answer on test $i."; break; fi
echo "accepted on test $i.";
done
}对拍。2> /dev/null 用于屏蔽 stderr 输出。
test_sample() {
l=$1; # 编号下界
r=$2; # 编号上界
code=$3;
file=$4; # 样例文件前缀
suf=$5; # 答案文件后缀
for ((i=$l;i<=$r;i++)) do
cp $file$i.in $code.in # 复制到 .in 文件
./$code; if [ $? != 0 ]; then echo "runtime error on sample $i."; continue; fi
diff $code.out $file$i.$suf -Z; if [ $? != 0 ]; then echo "wrong answer on sample $i."; continue; fi
echo "accepted on sample $i.";
done
}测试所有样例。
例:test_sample 1 3 cactus ex_cactus out。