본문 바로가기

Problem Solving/백준

백준 2309번

반응형
1. 7가지의 합이 100인 경우를 for문을 이용해 단순하게 다 해보았다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 
import java.util.Arrays;
import java.util.Scanner;
 public class Main {
    public static void main(String[] args)
    {
    Scanner sc= new Scanner(System.in);
    
    int count = 9;
    
    int[] arr = new int[count];
    
    while(count-- > 0) {
        arr[count] = sc.nextInt();    
    }
    Arrays.sort(arr);//배열을 미리 오름차순 정렬
    
    outerLoop:    
    for (int a=0; a<8; a++) { 
       for (int b=a+1; b<9; b++) { 
           for (int c=b+1; c<9; c++) { 
               for (int d=c+1; d<9; d++) { 
                   for (int e=d+1; e<9; e++) { 
                       for (int f=e+1; f<9; f++) { 
                           for (int g=f+1; g<9; g++) { 
                               if(arr[a]+arr[b]+arr[c]+arr[d]+arr[e]+arr[f]+arr[g] == 100) {
                                   //합이 100이라면 출력후
             System.out.println(arr[a]);
             System.out.println(arr[b]);
             System.out.println(arr[c]);
             System.out.println(arr[d]);
             System.out.println(arr[e]);
             System.out.println(arr[f]);
             System.out.println(arr[g]);
             break outerLoop;//반복문 전체 탈출
                 }
               } 
            }
        } 
    }
}
}
}
}
}
 
cs


2.  반대로 전체합에서 두 수를 뺀 경우, 합이 100일를 구해보았다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package b_2000번대;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main2309_2 {
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int max = 100;
        int size = 9;
        int sum = 0;
        int[] input = new int[size];
        
        for(int i=0; i < size; i++) {
            input[i] = Integer.parseInt(br.readLine());
            sum += input[i];
        }
        br.close();
        
        boolean checked = false;
        for(int i=0; i < size; i++) {
            for(int j = i+1; j < size; j++) {
                if(sum - (input[i] + input[j]) == max) {//전체합에서 두수를 뺀것이 100이라면
                    input[i] = Integer.MIN_VALUE;
                    input[j] = Integer.MIN_VALUE;
                    checked = true;
                    break;
                }
                if(checked) break;
            }
        }
        Arrays.sort(input);
        
        for(int value:input) { 
            if(value != Integer.MIN_VALUE) 
                System.out.println(value);
        }
    }
}
 
 
cs


반응형

'Problem Solving > 백준' 카테고리의 다른 글

백준 2941번  (0) 2019.01.26
백준 1152번  (0) 2019.01.26
백준 1316번  (0) 2019.01.24
백준 10809번  (0) 2019.01.24
백준 2908 상수  (0) 2019.01.20