본문 바로가기
백준(C언어)/22년 7월

7월 25일(월) - 6단계(2941번)

by C0MPAS 2022. 7. 25.

6단계 - 2941번

 

2941번: 크로아티아 알파벳

예전에는 운영체제에서 크로아티아 알파벳을 입력할 수가 없었다. 따라서, 다음과 같이 크로아티아 알파벳을 변경해서 입력했다. 크로아티아 알파벳 변경 č c= ć c- dž dz= đ d- lj lj nj nj š s= ž z=

www.acmicpc.net

 

문제점

1. 최초풀이

'ddz=z=' 과 'dz=ak' 의 출력값이 둘 다 2로 나오며, 정상 출력값보다 1이 작게 나온다

-> 'dz='에서 count -=2;로 표현했던 부분은 count -=1로 수정하며 완성

 

최초풀이

#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable: 4996)

#include <stdio.h>
 
int main(void)
{
	int count = 0;
	char word[100] = { 0, };
	scanf("%s", word);

	for (int i = 0; word[i] != '\0'; i++)
	{
		if (word[i] == '=')
		{
			if (word[i - 1] == 'c' ||  word[i - 1] == 's' || word[i - 1] == 'z')
			{
				count -= 1;
			}
			if (word[i - 2] == 'd' && word[i - 1] == 'z')
			{
				count -= 2;
			}
		}
		if (word[i] == '-')
		{
			if (word[i - 1] == 'c' || word[i - 1] == 'd')
			{
				count -= 1;
			}
		}
		if (word[i] == 'j')
		{
			if (word[i - 1] == 'l' || word[i - 1] == 'n')
			{
				count -= 1;
			}
		}
		count++;
	}
	printf("%d", count);

	return 0;
}

최종풀이

#define _CRT_SECURE_NO_WARNINGS
#pragma warning(disable: 4996)

#include <stdio.h>
 
int main(void)
{
	int count = 0;
	char word[100] = { 0, };
	scanf("%s", word);

	for (int i = 0; word[i] != '\0'; i++)
	{
		if (word[i] == '=')
		{
			if (word[i - 1] == 'c' ||  word[i - 1] == 's' || word[i - 1] == 'z')
			{
				count -= 1;
			}
			if (word[i - 2] == 'd' && word[i - 1] == 'z')
			{
				count -= 1;
			}
		}
		else if (word[i] == '-')
		{
			if (word[i - 1] == 'c' || word[i - 1] == 'd')
			{
				count -= 1;
			}
		}
		else if (word[i] == 'j')
		{
			if (word[i - 1] == 'l' || word[i - 1] == 'n')
			{
				count -= 1;
			}
		}
		count++;
	}
	printf("%d", count);

	return 0;
}

 

출처: https://www.acmicpc.net/problem/2941

댓글