以某個題爲例
Description
給出一個序列,包含n個數,有正數、0及負數。
現取連續的一段數,問它們和的絕對值的最小、最大值分別爲多少?
Input
第一行一個數n,表示序列長度。(1<=n<=1e5)
第二行包含n個整數,a[1],a[2]…a[n]。(-1e7<=a[i]<=1e7)
Output
一行包含兩個數 最小值、最大值。
Sample Input 1
4
-1 0 2 1
Sample Output 1
0 3
Hint
取[0]時,得到最小值0;取[2,1]時,得到最大值3。
總共四個程序
每個程序分別在一個文件裏,生成的所有數據都是放在對拍程序裏。a.in,a.out, ans.out。因爲程序是從對拍程序處運行的,所以其實在運行其它三個程序的時候他們的路徑是對拍程序的路徑,所以用相對路徑就可以了。
system(“C:\a.exe”) 運行a.exe可執行文件,路徑一定要正確啊.
system(“fc a.in” ans.in) 比較這兩個文件內容是否一直,相同返回0,不同返回非0.同樣需要指明這兩個文件的路徑。
以下程序僅供參考,不同的題目需對應修改
一 正確程序(確保答案是正確的道)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 100 * 1000+10;
ll n, a[N];
int main() {
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
cin >> n;
for (ll i = 1, x; i <= n; i++)cin >> x, a[i] = a[i - 1] + x;
sort(a, a + 1 + n, [](ll a, ll b) {return a > b; });
ll mi = 1e15;
for (int i = 1; i <= n; i++)mi = min(mi, abs(a[i] - a[i - 1]));
cout << mi << " " << abs(a[0] - a[n]) << endl;
return 0;
}
二 測試程序(需要測試的程序)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn = 1e5 + 5;
ll n, a[maxn], x;
int main()
{
freopen("a.in", "r", stdin);
freopen("ans.out", "w", stdout);
scanf("%lld", &n);
a[0] = 0;
for (ll i = 1; i <= n; i++) {
scanf("%lld", &x);
a[i] = a[i - 1] + x;
}
sort(a, a + n + 1);
ll mx = 1e15 + 5;
for (ll i = 1; i <= n; i++) {
mx = min(mx, a[i] - a[i - 1]);
}
printf("%lld %lld\n", mx, a[n] - a[0]);
return 0;
}
三 數據生成程序(根據題目生成相應的程序)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll random(int n) {
return (ll)rand()*rand() % n;
}
void creat() {
srand((unsigned)time(NULL));
freopen("a.in","w",stdout); //這裏的路徑是SureAnswer的路徑。因爲程序是從SureAnswer出運行的。
ll n = random(100000) + 1;
cout << n << endl;
for (int i = 1; i <= n; i++) {
ll x = random(20000000 + 1) - 10000000;
cout << x << " ";
}
}
int main() {
creat();
return 0;
}
四 對拍程序(比較測試程序與正確程序生成的答案是否相同)
#include<bits/stdc++.h>
using namespace std;
int main() {
for (int i = 1; i <= 20; i++) {
system("C:\\Users\\Administrator\\source\\repos\\CreatData\\Debug\\CreatData.exe");
double st = clock();
system("C:\\Users\\Administrator\\source\\repos\\Test\\Debug\\Test.exe");
double ed = clock();
system("C:\\Users\\Administrator\\source\\repos\\PL\\Debug\\PL.exe");
if (system("fc ans.out a.out"))
puts("Wrong Answer");
else printf("Accept,測試點 #%d,用時 %.0lfms\n",i,ed-st);
}
return 0;
}