如何快速的寫出對拍程序

前言

(dalao勿噴)ios

可能有不少小夥伴苦於本身造數據去驗證本身程序的正確性。
對此咱們珂以採用隨機造數據的方式,進行對拍驗證測試

但可能有不少小夥伴苦於不知道怎麼對拍
今天就介紹一種比較簡單的方法去實現對拍spa

第一步:放入要驗證的程序和暴力

找到本身的暴力和yy的正解, 分別放入std和test兩個cpp中
(以 A + B Problem 爲例)code

#include <iostream>  
using namespace std;    

int main()  
{   
    freopen("in.in","r",stdin);
    freopen("std.out","w",stdout);//這裏是std
    int a,b;    
    cin >> a >> b;   
    cout << a+b << endl;   
    return 0;    
}
#include <stdio.h>  

int main()   
{  
    freopen("in.in","r",stdin);
    freopen("test.out","w",stdout);//這裏是本身寫的暴力
    int a, b;  
    scanf("%d %d",&a, &b);  
    printf("%d\n", a+b);  
    return 0;  
}

第二步:造出隨機數據

而後寫一個用來創造隨機數據的cpp,起名爲dataci

#include<cstdio>
#include<cstdlib>
#include<ctime>
int main()
{
    freopen("in.in","w",stdout);//建立一個 in.in 文本文檔來存放咱們的隨機數據
    srand(time(0));
//  這是一個生成隨機數隨機種子,須要用到 ctime 庫
    printf("%d %d\n",rand(),rand());
//  這樣就生成了2個隨機數
    return 0;
}

第三步:進行文本對比

而後使用下面這個對拍程序資源

若是測試多組數據後程序仍然沒有退出,恭喜,您的正解極可能是對的
若是測試幾組數據後程序會中止運行,並會反饋兩個不同答案。
此時關掉正在運行的窗口,打開in.in文件,就能夠查看Hack數據了。文檔

#include<cstdio>
#include<cstdlib>
#include<ctime>
using namespace std;
int main()
{
//  int T = 0;
    while(1) //一直循環,直到找到不同的數據
    {
//      cout << ++T;能夠加一個計數器
        system("data.exe");//運行data.exe
	system("test.exe");//運行test.exe
	system("std.exe");//運行std.exe
	if(system("fc std.out test.out")) //當 fc 返回1時,說明這時數據不同
	break; //不同就跳出循環
    }
    return 0;
}

鳴謝

感謝 Imy_bisLy 提供的技術與資源支持io

相關文章
相關標籤/搜索