백준 2941 크로아티아 알파벳
c++문제풀이
문제
예전에는 운영체제에서 크로아티아 알파벳을 입력할 수가 없었다. 따라서, 다음과 같이 크로아티아 알파벳을 변경해서 입력했다.
크로아티아 알파벳 | 변경 |
---|---|
č | c= |
ć | c- |
dž | dz= |
đ | d- |
lj | lj |
nj | nj |
š | s= |
ž | z= |
예를 들어, ljes=njak은 크로아티아 알파벳 6개(lj, e, š, nj, a, k)로 이루어져 있다. 단어가 주어졌을 때, 몇 개의 크로아티아 알파벳으로 이루어져 있는지 출력한다.
dž는 무조건 하나의 알파벳으로 쓰이고, d와 ž가 분리된 것으로 보지 않는다. lj와 nj도 마찬가지이다. 위 목록에 없는 알파벳은 한 글자씩 센다.
입력
첫째 줄에 최대 100글자의 단어가 주어진다. 알파벳 소문자와 ‘-‘, ‘=’로만 이루어져 있다.
단어는 크로아티아 알파벳으로 이루어져 있다. 문제 설명의 표에 나와있는 알파벳은 변경된 형태로 입력된다.
출력
입력으로 주어진 단어가 몇 개의 크로아티아 알파벳으로 이루어져 있는지 출력한다.
- 제출 코드
#include <iostream>
#include <cstdio>
int main()
{
char str[102];
scanf("%s",str);
int count = 0;
for(int i = 0; str[i]; ++i)
{
if(str[i] != '=' && str[i] != '-'){
if(str[i] == 'd' && str[i+1] == 'z' && str[i+2] == '=')
{
i += 2;
}
else if( str[i] == 'l' || str[i] == 'n' )
{
if( str[i+1] == 'j')
i += 1;
}
++count;
}
}
printf("%d", count);
return 0;
}
- switch문 활용한 코드
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main()
{
string c_alphabet[8] = {"c=","c-","dz=","d-","lj","nj","s=","z="};
string input_str;
cin >> input_str;
int count = 0;
for(int i = 0; i <input_str.size(); ++i)
{
switch(input_str[i])
{
case 'c':
{
if(input_str[i+1] == '=')
{
++count;
++i;
continue;
}
else if(input_str[i+1] == '-')
{
++count;
++i;
continue;
}
}
break;
case 'd':
{
if(input_str[i+1] == 'z' && input_str[i+2] == '=')
{
++count;
i += 2;
continue;
}
else if(input_str[i+1] == '-')
{
++count;
++i;
continue;
}
}
break;
case 'l':
{
if(input_str[i+1] == 'j')
{
++count;
++i;
continue;
}
}
break;
case 'n':
{
if(input_str[i+1] == 'j')
{
++count;
++i;
continue;
}
}
break;
case 's':
{
if(input_str[i+1] == '=')
{
++count;
++i;
continue;
}
}
break;
case 'z':
{
if(input_str[i+1] == '=')
{
++count;
++i;
continue;
}
}
break;
}
if(input_str[i] >= 'a' && input_str[i] <= 'z')
{
++count;
}
}
printf("%d", count);
return 0;
}
-
- break
case를 작성할 때는 마지막 부분에서 break로 중단해주어야 해당 case만 실행
case를 break로 중단하지 않으면 그다음에 있는 case, default가 계속 실행
- break
-
- continue
continue is used in the scope of loops. A switch is not a loop.
If the the switch is inside in a loop then you can use continue - the continue command is associated with the loop - not the switch.
- continue