숫자 1, 2, 3으로만 이루어지는 수열이 있다. 임의의 길이의 인접한 두 개의 부분 수열이 동일한 것이 있으면, 그 수열을 나쁜 수열이라고 부른다. 그렇지 않은 수열은 좋은 수열이다.

다음은 나쁜 수열의 예이다.

  • 33
  • 32121323
  • 123123213

다음은 좋은 수열의 예이다.

  • 2
  • 32
  • 32123
  • 1232123

길이가 N인 좋은 수열들을 N자리의 정수로 보아 그중 가장 작은 수를 나타내는 수열을 구하는 프로그램을 작성하라. 예를 들면, 1213121과 2123212는 모두 좋은 수열이지만 그 중에서 작은 수를 나타내는 수열은 1213121이다.

입력

입력은 숫자 N하나로 이루어진다. N은 1 이상 80 이하이다.

출력

첫 번째 줄에 1, 2, 3으로만 이루어져 있는 길이가 N인 좋은 수열들 중에서 가장 작은 수를 나타내는 수열만 출력한다. 수열을 이루는 1, 2, 3들 사이에는 빈칸을 두지 않는다.

예제 입력 1

7

예제 출력 1

1213121

더보기

Solution

#include<stdio.h>
#include<stdbool.h>

int N, number[80]={0, };
bool found=false;

bool find_good(int index)
{
	for(int i=1;i<=index/2;i++)
	{
		bool same=true;

		for(int j=0;j<i;j++)
			if(number[index-j-1]!=number[index-i-j-1])
			{
				same=false;
				break;
			}

		if(same)
			return false;
	}

	return true;
}

void good_number(int index)
{
	if(found)
		return;
	if(index==1)
	{
		number[0]++;
		good_number(2);
	}
	else if(index==N+1)
	{
		if(find_good(index))
		{
			found=true;
			for(int i=0;i<N;i++)
				printf("%d", number[i]);
			printf("\n");
			return;
		}
		else
			find_good(index-1);
	}
	else
	{
		while(number[index-1]<3)
		{
			number[index-1]++;
			if(find_good(index))
			{
				good_number(index+1);
				break;
			}
		}
		if(!found)
		{
			number[index-1]=0;
			good_number(index-1);
		}
	}
}

int main(void)
{
	scanf("%d", &N);
	good_number(1);
	return 0;
}
728x90

+ Recent posts