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"

“ stdin”“ stdout” ,它们是“ sys”模块的一部分,我们可以对输入使用“ stdin”的 readline()方法,对输出使用“ stdout”的

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 '+'

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

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

输出量




如何在python中输出菱形 python编程输出菱形_如何在python中输出菱形


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

输出量


如何在python中输出菱形 python编程输出菱形_字符串_02


It takes 0.004445707999999993 sec

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