Posts Loops (Python 3)
Post
Cancel

Loops (Python 3)

  • URL : https://www.hackerrank.com/challenges/python-loops/problem

  • Task
    • The provided code stub reads and integer, n, from STDIN. For all non-negative integers i < n, print i^2.
  • Example
    • n = 3
    • The list of non-negative integers that are less than n = 3 is [0,1,2]. Print the square of each number on a separate line.
      1
      2
      3
      
      0
      1
      4
      
  • Input Format
    • The first and only line contains the integer, n.
  • Constraints
    • 1 <= n <= 20
  • Output Format
    • Print n lines, one corresponding to each i.

문제풀이

  • n이 주어지면 0부터 n까지의 리스트를 만들고, 해당 원소들의 제곱을 프린트하는 코드를 작성
  • 그냥 for문으로 돌리고, i는 제곱한것을 프린트하면된다. 매우 easy
1
2
3
4
if __name__ == '__main__':
    n = int(input())
    for i in range(n):
        print(i ** 2)
1
 5
This post is licensed under CC BY 4.0 by the author.