对拍,分为《自动对拍》和《手动对拍》,基础材料都是暴利解和尝试的正解程序,造数据这块,可以系统自动生成,也可以手动人为构造数据。

对拍,在提高组高分阶段是有用的,在普及组阶段,使用甚少。

参考:https://www.cnblogs.com/EdisonBa/p/13509379.html

新版

image.png

例题,Trailing Zeroes (III) LightOJ - 1138

  1. // duipai.cpp
  2. #include <bits/stdc++.h>
  3. //#include <unistd.h>
  4. using namespace std;
  5. int main(){
  6. int T = 50;
  7. system("g++ -o gen gen.cpp");
  8. system("g++ -o baoli baoli.cpp");
  9. system("g++ -o std A.cpp");
  10. for (int i = 1; i <= T; i++){
  11. system("./gen > data.in");
  12. system("./baoli < data.in > baoli.out");
  13. system("./std < data.in > std.out");
  14. if (system("diff baoli.out std.out")){
  15. printf("WA on test %d\n", i);
  16. return 0;
  17. }
  18. else{
  19. printf("AC on test %d\n", i);
  20. }
  21. //sleep(1);
  22. //usleep(500);
  23. }
  24. return 0;
  25. }
  1. // gen.cpp
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. int rnd(int l, int r){
  5. return l + rand() % (r - l + 1);
  6. }
  7. int main(){
  8. //freopen("data.in", "w", stdout);
  9. srand((int)time(0));
  10. int T = rnd(1, 5);
  11. printf("%d\n", T);
  12. while (T--){
  13. printf("%d\n", rnd(1, 1e2));
  14. }
  15. return 0;
  16. }
  1. // baoli.cpp
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. int T, q;
  5. int check(int x){
  6. int t = 0;
  7. while (x){
  8. t += x / 5;
  9. x /= 5;
  10. }
  11. return t;
  12. }
  13. int main(){
  14. //freopen("data.in", "r", stdin);
  15. //freopen("baoli.out", "w", stdout);
  16. //cout << check(1e9) << '\n';
  17. cin >> T;
  18. for (int i = 1; i <= T; i++){
  19. cin >> q;
  20. int j;
  21. for (j = 1; j <= 1e5; j++){
  22. if (check(j) == q) break;
  23. }
  24. if (check(j) == q) printf("Case %d: %d\n", i, j);
  25. else printf("Case %d: impossible\n", i);
  26. }
  27. return 0;
  28. }
  1. // A.cpp
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. int T, q;
  5. int check(int x){
  6. int t = 0;
  7. while (x){
  8. t += x / 5;
  9. x /= 5;
  10. }
  11. return t;
  12. }
  13. int main(){
  14. //freopen("data.in", "r", stdin);
  15. //freopen("std.out", "w", stdout);
  16. //cout << check(1e9) << '\n';
  17. cin >> T;
  18. for (int i = 1; i <= T; i++){
  19. cin >> q;
  20. int l = 0, r = 1e9, best = -1;
  21. while (l <= r){
  22. int mid = (l + r) >> 1;
  23. if (check(mid) >= q){
  24. best = mid;
  25. r = mid - 1;
  26. }
  27. else{
  28. l = mid + 1;
  29. }
  30. }
  31. if (check(best) == q) printf("Case %d: %d\n", i, best);
  32. else printf("Case %d: impossible\n", i);
  33. }
  34. return 0;
  35. }

旧版

image.png

生成数据

  1. // gen.cpp
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. int rnd(int l, int r){
  5. return l + rand() % (r - l + 1);
  6. }
  7. int main(){
  8. freopen("data.in", "w", stdout);
  9. srand((int)time(0));
  10. printf("%d %d\n", rnd(30, 50), rnd(70, 90));
  11. return 0;
  12. }

暴力程序和优化程序

  1. // A.cpp
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. int main(){
  5. freopen("data.in", "r", stdin);
  6. freopen("A.out", "w", stdout);
  7. int a, b;
  8. cin >> a >> b;
  9. cout << a + b << '\n';
  10. return 0;
  11. }
  12. // B.cpp
  13. #include <bits/stdc++.h>
  14. using namespace std;
  15. int main(){
  16. freopen("data.in", "r", stdin);
  17. freopen("B.out", "w", stdout);
  18. int a, b;
  19. cin >> a >> b;
  20. cout << a - b << '\n';
  21. return 0;
  22. }

对拍程序

  1. // duipai.cpp
  2. // linux下演示, diff命令
  3. // Windows下,是fc命令 system("fc A.out B.out");
  4. // cmd终端暂停查看,system("pause");
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. int main(){
  8. int T = 10;
  9. for (int i = 1; i <= T; i++){
  10. system("./gen");
  11. system("./A");
  12. system("./B");
  13. if (system("diff A.out B.out")){
  14. printf("WA on test%d\n", i);
  15. return 0;
  16. }
  17. else{
  18. printf("AC on test%d\n", i);
  19. }
  20. }
  21. return 0;
  22. }