알고리즘/부르트포스

[백준] 2309 : 일곱난쟁이

믕비 2023. 4. 6. 15:08

전체 키를 합한 후 100을 뺀 값을 저장.

더해서 위의 값을 만족하는 두 수를 저장한 후에 제외한 7개의 수를 오름차순으로 출력.

 

메모리 : 14092 KB

시간 : 124 ms

코드길이 : 1183 B

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

public class Main {

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		
		int total_height = 0;
		int[] height_9 = new int[9];
		
		for(int i = 0; i < 9; i++) {
			int height = Integer.parseInt(br.readLine());
			height_9[i] = height;
			total_height += height;
		}
		int heightOf2 = total_height - 100;
		int notMan_1 = 0;
		int notMan_2 = 0;
		for(int i = 0; i < 9; i++) {
			for(int j = 0; j < 9; j++) {
				if(height_9[i] + height_9[j] == heightOf2) {
					notMan_1 = height_9[i];
					notMan_2 = height_9[j];
					break;
				}
			}
		}
		
		ArrayList<Integer> arrlist = new ArrayList<>();
		
		for(int i = 0; i < 9; i++) {
			if(height_9[i] !=  notMan_1 && height_9[i] != notMan_2)
				arrlist.add(height_9[i]);
		}

		arrlist.sort(Comparator.naturalOrder());
		for(int i = 0; i < 7; i++) {
			sb.append(arrlist.get(i)).append('\n');
		}
		
		System.out.print(sb);
	}

}

1등코드

메모리 : 12768 KB

시간 : 68 ms

코드길이 : 761 B

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;

public class Main {
	
	static int sum = 0;
	
	public static int solution(int[] input) {
		
		for(int i=0;i<8;i++) {
			for(int j=i+1;j<9;j++) {
				if((sum-input[i]-input[j] == 100)) {
					input[i] = input[j] = 100;
					return 0;
				}
			}
		}
		return -1;
	}
	
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int[] input = new int[9];
		for(int i=0;i<9;i++) {
			input[i] = Integer.parseInt(br.readLine());
			sum += input[i];
		}
		
		solution(input);
		
		Arrays.sort(input);
		
		for(int i=0;i<7;i++) {
			System.out.println(input[i]);
		}
	}
}

'알고리즘 > 부르트포스' 카테고리의 다른 글

[백준] 1748 : 수 이어쓰기 1  (0) 2023.04.09
[백준] 6064 : 카잉 달력  (0) 2023.04.09
[백준] 1107 : 리모컨  (0) 2023.04.07
[백준] 1476 : 날짜계산  (0) 2023.04.07
[백준] 3085 : 사탕게임  (0) 2023.04.06