IT's 우

[java]백준 23842- 성냥개비 본문

알고리즘/백준

[java]백준 23842- 성냥개비

디우 2022. 9. 21. 15:05
728x90

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

 

23842번: 성냥개비

동빈이는 내일 TV 프로그램 '문제적 유니' 에 출연한다. 평소 애청자였던 동빈이는 성냥개비 문제가 자주 출제된다는 사실을 알았고, 오늘 예습하기로 했다. 성냥개비는 다음과 같이 숫자를 디지

www.acmicpc.net

 

코드

 

package javaCodingTest;

import java.util.*;

public class j23842 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt(); // 성냥의 개수
		// 숫자별ㅅ 필요한 성냥의 개수
		int[] count = new int[10];
		count[0] = 6;
		count[1] = 2;
		count[2] = 5;
		count[3] = 5;
		count[4] = 4;
		count[5] = 5;
		count[6] = 6;
		count[7] = 3;
		count[8] = 7;
		count[9] = 6;

		//0~99까지의 숫자에 필요한 성냥 개수
		int[] total_count = new int[100];
	
		int num = 0;

		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < 10; j++) {
				total_count[num] = count[i] + count[j];
				num++;
			}
		}
		long S_count = 0;
		boolean found = false;
		loop: for (int i = 0; i < 100; i++) {
			for (int j = 0; j < 100 - i; j++) {
				
				S_count = total_count[i] + total_count[j] + total_count[i + j] + 4; // i+j=(i+j)에는 i,+,=,(i+j) 성냥 필요 S_count는 전체성냥개수
				if (S_count == N) {
					found = true;
					if (i < 10) {
						System.out.print("0" + i + "+");
					} else
						System.out.print(i + "+");

					if (j < 10)
						System.out.print("0" + j + "=");
					else
						System.out.print(j + "=");

					if (i + j < 10)
						System.out.print("0" + (i + j));
					else
						System.out.print((i + j));
					break loop;

				}

			}

		}
		if (found == false)
			System.out.println("impossible");

	}

}

풀이

count[]  : 숫자 0~9 까지의 각 숫자에 필요한 성냥의 개수를 지정

 

total_count[]: 0~99까지의 숫자를 count[]를 사용하여 성냥의 개수를 구함(for문 사용)

 

중첩for문을 사용하여 i+j를 하여 i+j를 한 값 중 성냥을 N만큼 사용한 수식은 출력! 

728x90
반응형