对拍

· · 个人记录

对拍操作

step1 : 先打一个暴力( 略

step2 : 数据生成器

数据生成器一般都是生成随机数,但是你现在需要让数在一个范围内这是后写一个函数即可

int random(int l, int r) { return ((rand()*RAND_MAX+rand()) % (r-l+1) + l); }

随机种子可以选择按时间

srand(time(0));

所以差不多代码就是这样:

#include <bits/stdc++.h>
using namespace std;
inline int random(int l, int r) { return ((rand()*RAND_MAX+rand()) % (r-l+1) + l); }
signed main() {
  freopen("1.in","w",stdout);
  srand(time(0));
  /*
      -----
  */ 
  return 0;
}

step3:数据校验器

一般的数据校验器都是直接用fc(或diff)

但如果电脑有问题或者输出实数有误差就很难受,这时候手写校验器就很方便

spj主要在于同时打开多个文件读入这时候就需要用c++库中的神奇函数

FILE*f1 = fopen("1.out","rb");
if(!f1) return 1;
/*
  ----
*/
fclose(f1);
FILE*f2 = fopen("1.ans","rb");
if(!f2) return 1;
/*
  ----
*/
fclose(f2);

f1/f2之类的是文件指针,“rb”之类的都是固定格式,读入的话和scanf差不多,只是要用fscanf

fscanf(f1, "%d", &n); 

最后还可以根据不同情况返回不同的值

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int End = -1;
int n, m, TestA[N], TestB[N];
signed main() {
  FILE*f1 = fopen("1.out","rb");
  if(!f1) return 1;
  while(1) {
      fscanf(f1, "%d", &TestA[++n]); 
      if(TestA[n] == End) break;
      if(TestA[n] < 0) return 4;
  }
  fclose(f1);
  FILE*f2 = fopen("1.ans","rb");
  if(!f2) return 1;
  while(1) {
      fscanf(f2, "%d", &TestB[++m]);
      if(TestB[m] == End) break;
  }
  if(m != n) return 3;
  for(int i = 1; i <= n; i++) if(TestA[i] != TestB[i]) return 2;
  fclose(f2);
  return 0;
}

step4:对拍器

这一步很简单,就是每次要运行一个函数都用"system("");" 即可,甚至还可以加一些额外操作比如说给代码运行计时,返回不同的运行值,如果不为0就结束循环找到一组错误数据, 代码:

#include <bits/stdc++.h>
using namespace std;
signed main() { int a = 1, s;
    while(a) {
        printf("Test : %d \n", a++);
        system("data.exe");
        double st = clock();
        system("my.exe");
        double ed = clock();
        system("bf.exe");
        if((s = system("spj.exe"))) {
            printf("Wa %d \n", s); break;
        }
        else printf("AC time : %.3lfs\n", (ed - st) / 1000);
    }
    return 0;
}

step last:后续

考试中一定要使用对拍,哪怕暴力与暴力也行,只要把能得的分得到就行,一定要有好的心态,不要想着能切题,暴力打满就行