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;
}