c++程序对怕

以某个题为例
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;
}


更新

可以将每个测试数据保存下来。
在SureAnswer中打开文件,再运行exe文件之后读写会出错,必须在exe中打开文件,通过向main函数中传入参数就可以打开指定的文件了。参数的下标从1开始。
为了保证随机数的随性,在每次运行CreateData时,先先睡眠1秒,再用当前时间初始化随机数种子。

程序主要修改了 SureaAnswer,向其它exe参入的参数,第一个是读文件,第二个是写文件。

#include<bits/stdc++.h>
using namespace std;

int main() {
	for (int i = 1; i <= 5; i++) {
		string in = to_string(i) + ".in";
		string out = to_string(i) + ".out";
		string ans = "ans"+to_string(i) + ".out";
		char s1[10];
		strcpy(s1, in.c_str());
		char s2[10];
		strcpy(s2, out.c_str());
		char s3[10];
		strcpy(s3, ans.c_str());
		
		string s = "C:\\Users\\Administrator\\source\\repos\\CreatData\\Debug\\CreatData.exe";
		s = s + " " + s1+" %random%";
		system(s.c_str());
		
		s = "C:\\Users\\Administrator\\source\\repos\\Test\\Debug\\Test.exe";
		s = s + " "+s1+" "+s3;
		double st = clock();
		system(s.c_str());
		double ed = clock();
		
		s = "C:\\Users\\Administrator\\source\\repos\\PL\\Debug\\PL.exe";
		s = s + " " + s1 + " " + s2;
		system(s.c_str());
		

		s = "fc ";
		s = s + s2 + " " + s3;
		if (system(s.c_str())) {
			puts("Wrong Answer");
			return 0;
		}
		else printf("Accept,测试点 #%d,用时 %.0lfms\n",i,ed-st);
	}
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值