不带空格的.exe文件写法:
system("work.exe");
上面的方式相信大家都会,但当文件名含空格的时候,像下面这么写就会出问题
system("work 2.exe");
会报以下错误:
'work' 不是内部或外部命令,也不是可运行的程序
解决办法:添加 \" 、\" 将文件名括起来,代码如下。
system(" \"work 2.exe\" ");
应用(代码对拍实例)
以CF1359D 为例
make_data.cpp (造数据程序)
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int a[N], n;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
srand((unsigned)time(nullptr));
int n = 1 + rand() % 10;
cout << n << "\n";
for (int i = 1; i <= n; i++) {
cout << (rand() % 31) * (rand()%2?1:-1) << ' ';
}
cout << "\n";
return 0;
}
baoli.cpp (暴力程序)
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int a[N], n;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int ans = 0;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
for (int j = i; j <= n; j++) {
int Max = -30, sum = 0;
for (int k = i; k <= j; k++) Max = max(Max, a[k]), sum += a[k];
ans = max(ans, sum - Max);
}
}
cout << ans << "\n";
return 0;
}
D. Yet Another Yet Another Task.cpp (待对拍程序)
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int a[N], n;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int ans = 0;
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= 30; i++) {
int tmp = 0;
for (int j = 1; j <= n; j++) {
if (a[j] > i) {
tmp = 0;
continue;
}
else {
if (tmp > 0) tmp += a[j];
else tmp = a[j];
}
ans = max(ans, tmp - i);
}
}
cout << ans << "\n";
return 0;
}
duipai.cpp (对拍程序)
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
for (int i = 1;;i++) {
cout << i << ":" << endl;
system("make_data.exe > input.txt");
system("baoli.exe < input.txt > output.txt");
system(" \"D. Yet Another Yet Another Task.exe\" < input.txt > my.txt");
if (system("fc output.txt my.txt")) break;
}
return 0;
}