-
백준 알고리즘, 입출력 관련문제Algorithms/ACM_ICPC 2018. 7. 17. 12:11
개요
백준 알고리즘 사이트에는 여러 입출력 문제가 있다.
Problem Solving 을 시작하기 전에, 입출력에 관한 아주 조금의 불편함도 없어야 한다는 생각에
여러 입출력 문제를 모두 풀어내려고 한다.
이 글은 그 과정 중에서 주목할만한 부분만을 언급한다.
문제 리스트
2557, 1000, 2558, 10950, 10951, 10952, 10953, 11021, 11022, 11718, 11719, 11720, 11721, 2741, 2742, 2739, 1924, 8393, 10818, 2438, 2439, 2440, 2441, 2442, 2445, 2522, 2446, 10991, 10992
- 출처: http://plzrun.tistory.com/56 [plzrun's algorithm]주목할만한 부분
1. 10951
테스트케이스가 주어지지 않은 문제에서 파일의 끝을 파악하고자 할때 EOF (End Of File) 를 사용할 수 있다.
while(1) { cin >> a >> b; if(a < 0 || b > 10) break; cout << a+ b << endl; }
이렇게 작성된다면 while 문은 무조건 진입하게 되는 반면
while(cin >> a >> b) { if(a < 0 || b > 10) break; cout << a + b << endl; }
이 방식은 더 이상 입력이 발생하지 않는다면 while 문으로 진입하지 않는다.
cpp 에서 EOF 를 사용하는 방법이다.
scanf 를 사용한다면 다음과 같이 하면 된다.
while(scanf("%d %d", &a, &b) != EOF) { if(a < 0 || b > 10) break; cout << a + b << endl; }
2. 별 찍기 문제 (2438, 2439, 2440, 2441, 2442, 2445, 2522, 2446, 10991, 10992)
위의 번호들이 별찍기 문제이다.
대부분의 사람들의 경우 C 언어를 처음 접했을 때 별을 찍으면서 "나는 코딩에 재능이 없나보다" 라고 생각한다.
정말 대부분 그렇다.
그러나 마찬가지로 대부분은
Computer Science 와 Programming 을 배워나가면서 많은 시간동안 별찍기를 손대지 않아도
여러 분야에 걸쳐 내공이 쌓이면서
조금씩 별찍기 문제를 푸는 시간이 빨라지는 듯 하다.
위 문제들을 모두 해결하는데 1시간이 채 걸리지 않았다.
예전 같았으면 하루는 꼬박 걸렸을 듯 하다.
가장 어려웠던 10992 번 코드를 게재한다.
나머지 코드들은 깃헙 에 모두 올려놓았다.
/*** @site: https://www.acmicpc.net/problem/10992* @github: https://github.com/7772* @auth: Landon Park* @date: 2018. 07. 17*/#include <iostream>using namespace std;int main() {int N;int i, j, k, n, m;cin >> N;for (i = 1; i <= N; i++) {for (j = N; j > i; j--) {cout << " ";}for (k = 1; k <= ((2 * i) - 1); k++) {if (k >= 2 && k <= ((2 * i) - 2) && i != N) cout << " ";else cout << "*";}cout << endl;}return 0;}반응형'Algorithms > ACM_ICPC' 카테고리의 다른 글
백준 알고리즘, 11726 문제 (0) 2018.07.18 백준 알고리즘, 1463 문제 (0) 2018.07.17 백준 알고리즘, 1924 문제 (0) 2018.07.16 백준 알고리즘, 11723 문제 (1) 2018.07.13 백준 알고리즘, 11720 문제 (2) 2018.07.13