编写测试程序

辽宁师范大学 • 张大为@https://daweizh.github.io/noip/

在命令终端输入命令python --version,得到Noilinux的Python版本信息是

Python 2.7.6

Python编码采用2.x语法。

C++编写生成测试数据程序

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

int g = 20,n,m;
char name[30]="data";
char fname[50];
FILE *fin = NULL;
FILE *fans = NULL;

int main(){
    for( register int i=1;i<=g;i++){
        sprintf(fname,"%s%d.in",name,i);
        fin = fopen(fname,"w");

        sprintf(fname,"%s%d.ans",name,i);
        fans = fopen(fname,"w");

        srand(time(0));
		
        n = rand() % 1000 +1;
        m = rand() % 1000 + 1;
        fprintf(fin,"%d %d\n",n,m);

        for( register int j =1;j<=n;j++)
            fprintf(fin,"%d ",rand() % 50000 + 1);
        fprintf(fin,"\n");
		
        for(register int j=1;j<=m;j++)
            fprintf(fin,"%d\n",rand() % n + 1);
	
        printf("%s%d.in created.\n",name,i);

        fprintf(fans,"%d",rand() % 50000 + 1);
        printf("%s%d.ans created.\n",name,i);
		
        fclose(fans);
        fclose(fin);
    }	
    return 0;
}

C++语言编写对帕程序

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

int n=10;
char cmd[500];
char name[100]="score";

int main(){

    for(register int i=1;i<=n;i++){
        printf("%s%d\n",name,i);
        sprintf(cmd, "cp -f %s%d.in %s.in",name,i,name );
        system(cmd);
        sprintf(cmd,"./%s",name);
        system(cmd);
        sprintf(cmd,"diff -bB %s%d.ans %s.out",name,i,name);
        if(system(cmd)){
            printf("run %s%d.in error!\n",name,i);
        }else{
            printf("%s%d.in passed...\n",name,i);
            system("sleep 1");
        }
        printf("\n");
    }

    return 0;
}

Shell程序编写对拍

将下列程序保存成带.sh扩展名的试题名字,如score.sh

#!/bin/bash

name=$0				#read the name self
name=${name%\.*}	#only base name	
n=$1				#first parameters there is numer of datas

i=1
while [ $i -le $n ]
do
    echo $name$i
    cp -f $name$i.in $name.in
    ./$name
    if diff -bB $name$i.ans $name.out ; then
        echo Ac
    fi
    i=$(($i+1))
done

上述程序在终端下的执行方式是:

sh score.sh 20

Python 编写生成测试数据程序

import random

name = "data"
g = 20

for i in range(1,g+1):
    fin = open(name+str(i)+".in",'w+')
    fans = open(name+str(i)+".ans","w+")

    n = int(random.random()*1000+1)
    m = int(random.random()*1000+1)
    fin.write(str(n)+" "+str(m) + "\n")
	
    for j in range(1,n+1):
        x = int(random.random()*5000+1)
        fin.write(str(x)+ " ")
    fin.write("\n")
    fin.flush()

    for j in range(1,m+1):
        y = int(random.random()*n+1)
        fin.write(str(y)+"\n")
    fin.flush()
    print name+str(i)+".in created!"

    ans = int(random.random()*1000+1)
    fans.write(str(ans)+"\n")
    fans.flush()
    print name+str(i)+".ans created!"

    fans.close()
    fin.close()

Python编写读取测试数据程序

with open("data1.in","r") as fread:
    i = 1
    for line in fread:
        if i==1:
            temp = line.strip().split(' ')
            print "n=%d,m=%d" % (int(temp[0]),int(temp[1]))
        elif i==2:
            temp = line.strip().split(' ')
            for j in range(0,len(temp)):
                print int(temp[j]),
            print 
        else:
            print int(line.strip())
        i = i+1		

Python编写对拍程序

import os

n=10
name="score"

for i in range(1,n+1):
    print "%s%d\n" %(name,i),
    os.system("cp -f %s%d.in %s.in" % (name,i,name))
    os.system("./%s" % name)
    if os.system("diff -bB %s%d.ans %s.out" % (name,i,name)):
        print "run %s%d.in error\n" % (name,i)
    else:
        print "%s%d.in passed...\n" % (name,i)