1. 程式人生 > >Python 造資料,對拍利器

Python 造資料,對拍利器

0.簡介:

在Python環境下,利用random,或洛谷研發的Cyaron都是不錯的選擇。

如果要使用Cyaron 請參見

1.環境配置:

安裝Python3及以上版本,在安裝時選擇自定義,勾選自動設定環境變數選項。當然能自己搞%%%

安裝完成後開啟命令提示符,輸入python,如果進入Python環境就成功啦!!!

2.引入包:

想要使用random或cyaron,需要在程式開始引入包,類似於C++的標頭檔案。

import random
from cyaron import *

如果你是第一次使用,需要安裝cyaron。開啟命令提示符輸入以下命令即可。

pip install cyaron

3.格式化建立輸入輸出檔案

fout = open("brick.in","w")
fout.close()

建立brick.in

for i in range(1,11):
        fout = open("test%d.in"%i,"w")
fout.close()
#py的range返回值是左閉右開的

建立test1.in~test10.in

4.輸出

螢幕輸出

for i in range(1,6):
        for j in range(1,6):
                print("%d %d\n"
%(i,j))

py的%d,\n等用法類似C++
注意後半部分用%分割
樣例輸出

1 1
1 2
1 3
1 4
1 5
2 1
2 2
2 3
2 4
2 5
3 1
3 2
3 3

檔案輸出

接上一次的程式碼,向test1.in~test10.in中寫入

for i in range(1,11):
        fout = open("test%d.in"%i,"w")
        fout.write("%d "%i)
        #fout.write的用法與print類似
fout.close()

5.生成資料

random

import random
fout = open("brick.in","w") t = random.randint(1, 10) fout.write("%d\n"%t) for i in range(1,t+1): n = random.randint(1, 100) fout.write("%d\n"%n) for j in range(1,n+1): for k in range(1,n+1): p=random.randint(1,2) if p==1: fout.write(".") if p==2: fout.write("#") fout.write("\n") fout.close()

random有random.randint(l,r)函式,返回[l,r]之間的Int值。

樣例輸出:

5
23
#.#.....####..######.#.
.#..#.##.##.##...##..#.
####.###...##...#.#..#.
.#####.#......##...##.#
...###.####.#.##....###
.###.#.#.##.#..#...##..
#..####..#.###......###
#..#.##..##.#.##.#..#.#
####.##.##.##..#......#
#....#.#....##.#.##.###
...####...#.#.....#.###
##.#.#...###..#.#...###
#.###..##..#.##.######.
##..#.#..##....##.....#
.#.#.##.#.###..#.##...#
.#..###.#.###..#..##.#.
#..#.....##..###..#..#.
.#.#..######..##.#.####
##..##.#...###.#.##.#.#
##....##.....#...##...#
..#...#.##.###.......##
...#..#.##.###.###..##.
#.#....#####..#.####.#.
25
#..#.#.#####.##..##..#.##
..#.#..##.#.#.#.#..###..#
####..#.###.#####.#...##.
###....#...#..##.#..##.#.
#..##.##..#..##..####...#
.#....#......##..#.#.###.
#...##.##.....#..##.##...
.##..######.#.#..#####..#
##.....#####.####.###..#.
.####...##...#.#.###.##..
####.#..####..#.##...#.#.
###..##....##..###.#.#.##
#......#.######.#..#...##
##.#.####..##.#.#.#..#.##
..###.##.####.####..#..##
.####.#####..#..#.#.#....
.#.#.###.#.##.#..#####..#
#.#...#.####.####.#..#.##
##..##...#.###.#...#.##.#
###..##..##.####..##..###
.#.####.#..####....#####.
##......###...##.##.#.##.
###.#.#..#.##...###.##.#.
#..#.##.#...##.#.#...#...
###..##....#....####..##.

資料太多不完全展示

6.對拍

對拍需要系統包

import os

OS命令用法與c++的system命令類似,都是引用系統命令提示符的命令

import os
while True:
        os.system("a.exe")
        os.system("b.exe")
        os.system("fc a.out b.out")

當然你也可以用C++

#include <cstdlib>
using namespace std;
int main(){
    while (1){
        system"python mkdt.py");
        system("a.exe");
        system("b.exe");
        system("fc a.out b.out");
    }
    return 0;
}

對於不需要寫檔案輸入輸出的對拍Cyaron有更好的解決方案。它有自帶的對拍函式可以引用。

7.Summary

當然了,上面的操作都是要設定好Python的環境變數的。

如果你對Python的基本語法還有不理解的地方,請參考luogu Python入門指南