BOJ | 2742번

송치헌·2021년 6월 11일
0
post-thumbnail

Python 풀이

N = int(input())

for i in range(N,0,-1):
    print(i)
for i in range(N,0,-1)

이것은 N에서 0이 되기 전까지 iterator가 -1씩 증가하며 반복하라는 뜻이다.
즉 정수 i가 N부터 1까지 1씩 빼지면서 반복문을 돌게 된다.
예를 들어서

>>> for i in range(0,11,2):
...     print(i)
... 
0
2
4
6
8
10

라고 한다면 '0에서 11전까지(10까지) 2씩 증가시키며 반복문을 실행하여라' 라는 뜻이다. i는 0, 2, 4, 6, 8, 10 이 된다.

C++ 풀이

#include <iostream>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    for (int i = n; i >= 1; i--) {
    	cout << i << '\n';
    }
}
profile
https://oraange.tistory.com/ 여기에도 많이 놀러와 주세요

0개의 댓글