DEV Community

Abu Hurayra
Abu Hurayra

Posted on

Python Input/Output for Competitive Programming

Python has become one of the most popular languages to use for algorithmic problem solving and competitive programming. Despite having some downsides due to its slower speed, its popularity is increasing due to the simplicity of python syntax and easier learning curve.

In this post, we will learn how to take input and print output for various competitive programming problems.


Example - 01

Problem Link: 1352C. K-th Not Divisible by n

image of Codeforces 1352C. K-th Not Divisible by n

The problem statement says that the first line will contain one integer t. Then there will be t lines. Each line will have 2 integers, n and k.

We can at first take t as input, then run a loop. In each turn of the loop, we can take n and k.

The output is a single number for each n and k. We can use the print() function to print the output.

Here is the code:

t = int(input())

for i in range(t):

    n, k = map(int, input().split())

    # Solve the problem here

    # Write output here
    print(solution)
Enter fullscreen mode Exit fullscreen mode

This is the complete solution to the problem:

import math

t = int(input())

for i in range(t):

    n, k = map(int, input().split())

    solution = k + math.floor((k-1)/(n-1))

    print(solution)
Enter fullscreen mode Exit fullscreen mode

Example - 02

Problem Link: 1352C. K-th Not Divisible by n

image of Codeforces 1352C. K-th Not Divisible by n

Top comments (0)