python编程输出菱形_快速输入/输出,可在Python中进行有竞争力的编程

python编程输出菱形

In competitive programming it is very important to make the program time efficient. And to make that one major problem is input and output. As input and output of the program occupy more time. So, because of that, the efficiency of code get decreased.

在竞争性编程中,使程序高效运行非常重要。 为了使这一主要问题成为输入和输出。 由于程序的输入和输出占用更多时间。 因此,因此,代码的效率降低了。

There are some of the specific functions that can be used for fast input and output.

有一些特定功能可用于快速输入和输出

Python provides two file objects "stdin" and "stdout" while are a part of "sys" module, we can use readline() method of "stdin" for input and write() function of "stdout" for output.

Python提供了两个文件对象“ stdin”“ stdout” ,它们是“ sys”模块的一部分,我们可以对输入使用“ stdin”的 readline()方法,对输出使用“ stdout”的 write()函数。

1) stdin.readline()

1)stdin.readline()

It is used to take the input, it takes the input as a string by default and if you want to take input as an integer then use the eval() or int() functions. If you want to take space separated inputs in a single line then use split().

它用于接收输入,默认情况下将输入作为字符串,如果您想将输入作为整数,则使用eval()或int()函数。 如果要在一行中使用空格分隔的输入,请使用split() 。

Example:

例:

    stdin.readline(eval(input().split()))

instead of eval() you can also use int(). If you do not use any of these functions, input will be string by default

除了eval()之外,您还可以使用int() 。 如果您不使用任何这些功能,则默认情况下,输入将为字符串

2) stdout.write()

2)stdout.write()

It gives the output in string type. And if you want the multiple outputs in a single statement, use '+' in the between the two.

它以字符串类型给出输出。 如果要在单个语句中提供多个输出,请在两者之间使用“ +” 。

Example:

例:

    stdout.write('p', + ,'A', +, 'C')

Syntaxes:

语法:

    stdin.readline()    # for input
    stdout.write()      # for output

To use the fast input output functions, you need to import the files which contain them i.e. "sys module".

要使用快速输入输出功能,您需要导入包含它们的文件,即“ sys module”

Example 1: Python code for simple input, output

示例1:用于简单输入,输出的Python代码

# python program without fast I/O
from time import perf_counter

#integer input from user, 2 input in single line
n,m = map(int,input().split()) 

t1_start = perf_counter()
for i in range(n):
    t=int(input()) # user gave input n times
    if t%m == 0:
        print(t) #print the output if condition satisfied

t1_stop = perf_counter()# Stop the stopwatch/counter

print("Elapsed time:", t1_stop-t1_start) # Report results

Output

输出量

output 1

Example 2: Python code for fast input, output

示例2:用于快速输入,输出的Python代码

# python program with fast I/O

#module contain stdin ,stdout
from sys import stdin, stdout 
from time import perf_counter

#integer input from user ,2 input in single line
n,m=map(int,input().split()) 

t1_start = perf_counter()

for i in range(n):
    t=int(stdin.readline()) # input using fast i/p method 
    if t%m == 0:
        stdout.write(str(t)+'\n') #input using fast o/p method

t1_stop = perf_counter()# Stop the stopwatch

print("Elapsed time:", t1_stop-t1_start) # Report results


Output

输出量

output 2

It takes 0.004445707999999993 sec time to execute from input to output

从输入到输出需要0.004445707999999993秒的时间

Note: As you can see in the above code, that time taken by fast input method to run a 100 digit line is half the time taken in simple Input/Output.

注意:如上面的代码所示, 快速输入法运行100位数字行所花费的时间是简单Input / Output所花费的时间的一半

Recommended posts

推荐的帖子

翻译自: https://www.includehelp.com/python/fast-input-output-for-competitive-programming-in-python.aspx

python编程输出菱形

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值