记录第一次写对拍

103 阅读1分钟

对拍

所谓对拍就是算法里面用来测试的一种算法,通过构造大量随机数来测试程序,比用极限的数据来debug还要有效。

具体做法

要先生成随机数,这里是多组样例,有个k(样例的个数),ab则是我们需要输入的两个数

#include<bits/stdc++.h>
using namespace std;
// N 为数据个数,MAX 为数据上限 
const int N = 1e4, MAX = 1e5 + 1;
string choice = "aAbBcCDdeEfGgHhiIJjkKLlIimMnNOopPRrQqsStTUuVvwWxXyYzZ";
int main(void)
{
    // 设置 cout printf 这些输出流都输出到 test.in 里面去
    srand((int)time(NULL));
    freopen("E://1.in", "w", stdout);
    // 随机生成 N 个数字
    int k=300;
    cout<<k<<endl;
    while(k--){
    	int a=rand()%2001+2000;
    	int b=rand()%2001+2000;
		cout<<a<<' '<<b<<endl;
	}
    
    // 关闭输出流
    fclose(stdout);
    return 0;
}

然后是打开这个文件,再将结果输出到另一个文件里面

#include<iostream>
#include<cmath>
using namespace std;
int main(void)
{
    // 设置 cin scanf 这些输入流都从 test.in 中读取
    freopen("E://1.in", "r", stdin);
    // 设置 cout printf 这些输出流都输出到 test.out 里面去
    freopen("E://1.out", "w",stdout);
    // 在下面写待测程序,即标程
    long long a,b,k;
    cin>>k;
	for(int i=0;i<k;i++){
	cin>>a>>b;
    cout<<a*a/4*(b*b/4)<<endl;
	}
    // 关闭输入流
    fclose(stdin);
    // 关闭输出流
    fclose(stdout);
    return 0;
}

运用的是freopen("E://1.in", "r", stdin);freopen("E://1.out", "w",stdout);将标准输入输出函数写入文件中。