문제 출처: www.acmicpc.net/problem/2742
2742번: 기찍 N
자연수 N이 주어졌을 때, N부터 1까지 한 줄에 하나씩 출력하는 프로그램을 작성하시오.
www.acmicpc.net
1. 코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main(void) | |
{ | |
cin.tie(NULL); | |
ios::sync_with_stdio(false); | |
int N; | |
cin >> N; | |
for(int i = N; i > 0; i--) | |
cout << i << '\n'; | |
return 0; | |
} |
2. 풀이
cin.tie(NULL);
ios::sync_with_stdio(false);
이 코드를 이용하여 버퍼로 인한 지연 속도를 최소화시켜서 더 빠르게 실행될 수 있게 한다. 만약 이것을 사용하지 않는다면 속도가 너무 느려서 시간 초과에 걸리게 된다.
int N;
cin >> N;
그리고 N을 입력을 받는다.
for(int i = N; i > 0; i--)
cout << i << '\n';
그리고 반복문을 이용하여 N값부터 시작하여서 1까지 출력을 하게 한다. 그런데 여기서 개행 작업을 endl로 하는 것보다는 \n이 더 빠르게 실행되기 때문에 \n을 이용하여 출력한다.
'컴퓨터 > 백준 알고리즘' 카테고리의 다른 글
백준 알고리즘 2439번: 별 찍기 - 2 [C++] (0) | 2020.12.15 |
---|---|
백준 알고리즘 11021번: A+B - 7 [C++] (0) | 2020.12.08 |
백준 알고리즘 15552번: 빠른 A+B [C++] (0) | 2020.12.04 |
백준 알고리즘 8393번: 합 [C++][재귀] (0) | 2020.12.03 |
백준 알고리즘 10950번: A+B - 3 [C++] (0) | 2020.12.01 |