C++
使用system
在linux下,终端shell语言
#include<bits/stdc++.h>
using namespace std;
int main()
{
system("sudo g++ 1.cpp -o my");
system("sudo g++ 2.cpp -o std");
system("sudo g++ 3.cpp -o ran");
while (1){
system("./ran > data");
system("./my < data > my.out");
system("./std < data > std.out");
if (system("diff std.out my.out")){
cout << "WA" << endl;
break;
}
else cout << "AC" << endl;
}
return 0;
}
shell语言
【Shell脚本攻略】-shell中各种括号()、(())、[]、[[]]、{}的作用
Shell $0, $#, $*, $@, $?, $$和命令行参数
#!/bin/bash
g++ random.cpp -o random
g++ my.cpp -o my
g++ std.cpp -o std
i=1
while true; do
i=$((i+1))
./random > in${i}.in
./std < in${i}.in > std.out
./my < in${i}.in > my.out
diff std.out my.out
if [ $? -ne 0 ]
then
echo test ${i} WA
break
else
echo test ${i} AC
fi
done
测运行时间我是拒绝的 <- time ./a
就可以测速度了。。。
upd.2019.10.29 感谢 ymz 的提醒。
Bat
还是BATCH好玩
@echo off
g++ std.cpp -o std
g++ my.cpp -o my
g++ ran.cpp -o ran
set /a i=0
:loop
set /a i=%i%+1
ran > tst.txt
my < tst.txt > my.out
std < tst.txt > std.out
fc /w my.out std.out
if not errorlevel 1 goto aa
goto bb
:aa
echo test %i% AC
goto loop
:bb
echo test %i% WA
pause
测试运行时间
测时间还是在c++内部搞搞好。
#include<cstdio>
#include<ctime>
#include<windows.h>
// #include<unistd.h> in linux
using namespace std;
int main()
{
clock_t st, ed;
double cost;
st = clock();
// main
ed = clock();
cost = ((double)(ed-st))/CLOCKS_PER_SEC;
printf("%lf", cost);
return 0;
}
SPJ
@echo off
g++ my.cpp -o my.exe
g++ spj.cpp -o spj.exe
set i=-1
:loop
set /a i=%i%+1
echo %i% > bala.out
call my.exe < bala.out > my.out
call spj.exe < my.out > spj.out
fc /w spj.out bala.out
if not errorlevel 1 (
echo test %i% AC
goto loop
)
echo test %i% WA
pause
上面是一道例题的spj:构造一个迷宫使(0, 0) -> (n-1, m-1) 的最短路长度是k。(k <= 1000, n, m <= 50)
构造程序:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 100;
int k;
int n, m, rem, mp[N][N];
void Initialize(int row)
{
memset(mp, 0, sizeof(mp));
for (int i = 1; i <= 49; i += 2)
for (int j = 1; j <= row; j++)
mp[j][i] = 1;
for (int i = 2; i <= 49; i += 2)
mp[(i>>1&1 ? row : 1)][i] = 1;
}
int main()
{
cin >> k;
if (k <= 49){
cout << "1 " << k+1 << endl;
for (int i = 1; i <= k+1; i++) cout << '.'; cout << endl;
return 0;
}
if (k <= 98){
int nn = (k+2)/2, mm = k+2-nn;
cout << nn << " " << mm << endl;
for (int i = 1; i <= mm; i++) cout << '.'; cout << endl;
for (int i = 2; i <= nn; i++){
for (int j = 1; j < mm; j++)
cout << '#';
cout << '.' << endl;
}
return 0;
}
if (k&1^1){ // k even
n = 49;
m = 49;
rem = 49*49-24*48-k;
Initialize(49);
int i = 2;
while (rem){
if (48*2 <= rem){
mp[1][i] = 1;
rem -= 48*2;
i += 4;
}
else{
rem >>= 1;
mp[49-rem][i] = 1;
rem = 0;
}
}
}
else{ // k odd
n = 50;
m = 49;
rem = 50*49-24*49-k;
Initialize(50);
int i = 2;
while (rem){
if (49*2 <= rem){
mp[1][i] = 1;
rem -= 49*2;
i += 4;
}
else{
rem >>= 1;
mp[50-rem][i] = 1;
rem = 0;
}
}
}
cout << n << " " << m << endl;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++)
cout << (mp[i][j] ? '.' : '#');
cout << endl;
}
return 0;
}
spj:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int N = 100;
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, 1, -1};
int n, m, mp[N][N], dis[N][N];
queue<pair<int, int> > q;
int main()
{
cin >> n >> m;
memset(mp, 0, sizeof(mp));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++){
char c = getchar();
while (c != '.' && c != '#') c = getchar();
mp[i][j] = (c == '.');
}
q.push(make_pair(1, 1));
dis[1][1] = 0;
while (!q.empty()){
int x = q.front().first;
int y = q.front().second;
q.pop();
for (int i = 0; i < 4; i++){
int nx = x+dx[i];
int ny = y+dy[i];
if (mp[nx][ny]){
q.push(make_pair(nx, ny));
dis[nx][ny] = dis[x][y]+1;
mp[nx][ny] = 0;
}
}
}
cout << dis[n][m] << endl;
return 0;
}
很快就把所有的输入数据全都跑完了,稳~