백준 알고리즘 단계별/while 문
백준 BOJ C# [10951] [A+B - 4]
재호우96
2020. 1. 15. 20:06
반응형

백준 BOJ C# [10951] [A+B - 4]
출제 링크 : https://www.acmicpc.net/problem/10951
10951번: A+B - 4
두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.
www.acmicpc.net
코드
This file contains hidden or 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
using System; | |
using static System.Console; | |
using System.Text; | |
using System.IO; | |
namespace baekjoon | |
{ | |
class MainApp | |
{ | |
static void Main(string[] args) | |
{ | |
// failure ..? why | |
/*while (true) | |
{ | |
string[] input = Console.ReadLine().Split(); | |
if (input == null) break; | |
int numA = int.Parse(input[0]); | |
int numB = int.Parse(input[1]); | |
int addNum = numA + numB; | |
WriteLine(addNum); | |
}*/ | |
// success!! | |
while (true) | |
{ | |
string input = Console.ReadLine(); | |
if (input == null) break; | |
string[] inputNum = input.Split(); | |
int numA = int.Parse(inputNum[0]); | |
int numB = int.Parse(inputNum[1]); | |
int addNum = numA + numB; | |
WriteLine(addNum); | |
} | |
} | |
} | |
} |
이번 문제는 EOF(End of File)을 요구한다. 간단하게 파일의 끝을 확인할 수 있나? 를 묻는다..
간략하게 if문으로 input에 입력값이 없어 null상태가 되면 무한루프를 빠져나갈 수 있게 만들었다.
근데 처음 입력을 받을 때 string[] 배열로 받아서 바로 Split을 해주게 되면 런타임 에러가 뜨게 되는데
그냥 string으로 받게되면 통과된다.. why..?
반응형