티스토리 뷰
try ~ catch 문장의 소중함을 알게 된 문제였다.
크게 두가지 방법으로 가능한 듯 한데,
1. count는 0부터 시작, 크로아티아 알파벳에 해당하는 조합이 나오면 pointer를 +1 시킴
2. count는 length부터 시작, 크로아티아 알파벳에 해당하는 조합이 나오면 count를 -1 시킴 (or -2)
나는 1번으로 풀었다.
try~catch를 배우기나 했지 실제로 안써봤던 건데, 이런 상황에서 index가 array보다 튀어나오는 경우 요긴하게 쓰겠더라...
char[] croatia = Console.ReadLine().ToCharArray(); int count = 0; for (int pointer = 0; pointer < croatia.Length; pointer++) { try { switch(croatia[pointer]) { case 'c': if (croatia[pointer + 1] == '=') { pointer++; } else if (croatia[pointer + 1] == '-') { pointer++; } break; case 'd': if (croatia[pointer + 1] == 'z') { if (croatia[pointer + 2] == '=') { pointer += 2; } } else if (croatia[pointer + 1] == '-') { pointer++; } break; case 'l': if (croatia[pointer + 1] == 'j') { pointer++; } break; case 'n': if (croatia[pointer + 1] == 'j') { pointer++; } break; case 's': if (croatia[pointer + 1] == '=') { pointer++; } break; case 'z': if (croatia[pointer + 1] == '=') { pointer++; } break; } }catch(IndexOutOfRangeException) { count++; continue; } count++; } Console.WriteLine(count);
좀 줄긴 줄었다. 코드 길이는 더 줄이는 것 가능한데 그게 큰 의미는 없는 것 같아서..
'■ 알고리즘 > ◻ 백준' 카테고리의 다른 글
[C#]백준 1157번 : 단어 공부 (0) | 2018.08.14 |
---|---|
[C#]백준 1316번 : 그룹 단어 체커 (0) | 2018.08.13 |
[C#]백준 1032번 : 명령 프롬프트 (0) | 2018.08.13 |
[C#]백준 2908번 : 상수 (0) | 2018.08.13 |
[C#]백준 1159번 : 농구 경기 (0) | 2018.08.13 |