백준 문제풀이/실버5

백준 1475번 - 방 번호

void_melody 2021. 11. 14. 01:47

https://www.acmicpc.net/problem/1475

 

1475번: 방 번호

첫째 줄에 다솜이의 방 번호 N이 주어진다. N은 1,000,000보다 작거나 같은 자연수이다.

www.acmicpc.net

6과 9를 하나의 숫자로 보는 것이 핵심이다.

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string input;
	cin >> input;
	int length = input.length();

	
	char arr[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
	int result[10] = { 0, };

	for (int i = 0; i < length; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			if (input[i] == arr[j])
			{
				result[j]++;
				break;
			}
		}
	}

	result[6] += result[9];
	result[6] = (result[6] + 1) / 2;
	result[9] = 0;
		
	int max = 0;
	for (int n = 0; n < 10; n++)
	{
		if (max < result[n])
			max = result[n];
	}

	cout << max;

	return 0;
}

예를 들어 66699 라면, 69는 한세트 이므로, 3개가 나온다.

669 라면? 2개가 필요하겠지. 이를 활용하기 위해

    result[6] += result[9];
	result[6] = (result[6] + 1) / 2;
	result[9] = 0;

6과 9의 수를 더해서 2를 나누어주었다.

예를 들어 더한 값이 5면, 2를 나누면 2가 나오지만 우리가 원하는 값은 엄연히 3이기에 +1로 값을 맞추어주었다.

이거랑 같은 소리이다.

result[6] += result[9];
	if (result[6] % 2 == 0)
		result[6] = result[6] / 2;
	else
		result[6] = (result[6] / 2) + 1;
	result[9] = 0;