반응형

모든 경우의 수를 구해야 계산을 할 수있다.

예를 들어 집합의 크기가 3일 경우 

000, 001, 010, 011, 100, 101, 110, 111 의 8가지 경우의 수를 계산해서 더해야 한다.

비트 연산을 통해 모든 경우의 수를 계산했다.

비트 연산은 다시 블로그에 작성을 해야겠다.

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
import java.io.FileInputStream;
import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
 
        Scanner sc = new Scanner(new FileInputStream("input.txt"));
        //Scanner sc1 = new Scanner(System.in);
        int n = sc.nextInt();
        int s = sc.nextInt();
        int[] arr = new int[n];
        int result = 0;
        int sum = 0;
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }
        //i는 0000~1111까지
        for (int i = 1; i < 1 << n; i++) {
 
            int bit = i;
            //0000 을 
            //bit!=0 은 도중에 할게 없으면 컷 0000111 일떄 1뽑고 난후 0이므로 돌 필요가 없다.
            for (int j = 0; bit != 0; j++, bit >>= 1) {
 
 
                //첫번째 자리가 0이면 j는 구할필요가 없다. ex) 10010일때 j의 자리는 0이므로 뽑을 필요가 없기 때문이다.
                if ((1 & bit) == 0){
                    continue;
                }
                sum = sum + arr[j];
            }
            if (sum == s) {
                result++;
            }
            sum = 0;
        }
        System.out.println(result);
 
    }
 
}


반응형
반응형

단, B에 있는 수는 재배열하면 안된다. 라는 문구가 있는데 실제 구현을 할때는 정렬로 재배열을 해줘야 한다.

예를 들어 1 2 3 4 5인 A 배열과 1 3 5 7 9 인 B 배열이 있을 때 가장 작은 수가 나올 경우는 

서로 역방향이 되어 큰값과 작은값이 곱해질 때이다. 정확히 1*9 + 2*7 + 3*5 + 4*3 + 5*1인 경우이다.

결국, A 배열과 B배열을 정렬한 다음 A와 B의 역방향으로 곱해주면 된다.


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
import java.util.Arrays;
import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
 
        Scanner sc = new Scanner(System.in);
 
        int n = sc.nextInt();
 
        int[] A = new int[n];
        int[] B = new int[n];
 
        for (int i = 0; i < n; i++) {
            A[i] = sc.nextInt();
        }
        for (int i = 0; i < n; i++) {
            B[i] = sc.nextInt();
 
        }
        Arrays.sort(A);
        Arrays.sort(B);
        int sum = 0;
        for (int i = 0; i < n; i++) {
            sum += A[i] * B[n - 1 - i];
        }
        System.out.println(sum);
 
    }
}



반응형
반응형

자료구조를 사용하는데 엄청 어려움을 겪었다... 문제 자체는 어렵지 않다.

물론 난 자료구조를 아예 모르기 때문에 인터넷을 뒤져가면서 코딩을 했고 거의 내가 푼게 아니라 남걸 베껴온 수준이지..하하


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
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Scanner;
import java.util.Set;
 
public class Main {
 
    private static Scanner sc;
 
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
 
        sc = new Scanner(new FileInputStream("input.txt"));
        // sc = new Scanner(System.in);
        int n = sc.nextInt();
 
        Set<String> set = new HashSet<>();    //set을 사용함
        for (int i = 0; i < n; i++) {
            String name = sc.next();
            String commute = sc.next();
 
            if (commute.equals("enter")) {    //enter면 set에 추가를 하고
                set.add(name);
            } else {                        //leave면 set에서 삭제를 한다. 물론 leave밖에 없기에 else문을 사용했다.
                set.remove(name);
            }
        }
        List<String> array = new ArrayList<String>(set);    //sort와 ListIterator를 사용하기 위해 set --> list 작업을 했다.
        Collections.sort(array);    //정렬
        ListIterator<String> iter = array.listIterator(array.size());    //ListInterator를 사용해서 반대로 탐색
 
        
        //Iterator 사용
        while (iter.hasPrevious()) {
            System.out.println(iter.previous());
        }
 
    }
 
}



내가 공부할 부분은 Set, Map, List, Collections, Iterator 정도 되겠다.

반응형
반응형


딱히 설명 할게 없을 것 같다. 자료구조 스택을 이용 하면 쉽게 답을 얻을 수 있다.

split으로 String과 int 부분을 나누는게 가장 어려운 듯 하다.


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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.io.FileInputStream;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
 
public class Main {
 
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
 
        // Scanner sc = new Scanner(System.in);
        Scanner sc = new Scanner(new FileInputStream("input.txt"));
        int n = sc.nextInt();
        sc.nextLine();
        Stack<Integer> stack = new Stack<Integer>();
        for (int i = 0; i < n; i++) {
            String str = sc.nextLine();
 
            String instruction[] = str.split(" ");
 
            switch (instruction[0]) {
            case "push":
                stack.push(Integer.parseInt(instruction[1]));
                break;
 
            case "pop":
                if(stack.isEmpty()){
                    System.out.println(-1);
                }
                else{
                    System.out.println(stack.pop());
                }
                break;
 
            case "size":
 
                    System.out.println(stack.size());
                break;
 
            case "empty":
                if(stack.isEmpty()){
                    System.out.println(1);
                }
                else{
                    System.out.println(0);
                }
                break;
 
            case "top":
                if(stack.isEmpty()){
                    System.out.println(-1);
                }
                else{
                    System.out.println(stack.peek());
                }
                break;
 
            }
 
        }
 
    }
 
}
cs


반응형
반응형


생각할게 한가지 있었는데 어떻게 마지막 값을 뽑아 올것인지에 대한 Back 명령어였다.

Queue는 선입선출이기 때문에 아주 단순히 Push를 해줄 때마다 값을 저장해주면  마지막값으로 저장된다는 생각을 갖고 'last' 라는 변수를 통해

마지막값을 저장해주었다.

-끝-

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.io.FileInputStream;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
 
        // Scanner sc = new Scanner(System.in);
        Scanner sc = new Scanner(new FileInputStream("input.txt"));
        int n = sc.nextInt();
        sc.nextLine();
 
        Queue q = new LinkedList();
        int x;
        int last = 0; //마지막값을 위한 변수
        for (int i = 0; i < n; i++) {
            String str = sc.nextLine();
 
            String instruction[] = str.split(" ");
 
            switch (instruction[0]) {
            case "pop":
                if (q.isEmpty()) {
                    System.out.println("-1");
                } else {
                    System.out.println(q.poll()); // 제거 하며 읽기
                }
 
                break;
 
            case "push":
                x = Integer.parseInt(instruction[1]);
                last = x;
                q.offer(x);
                break;
 
            case "size":
                System.out.println(q.size());
                break;
 
            case "empty":
                if (q.isEmpty()) {
                    System.out.println("1");
                } else {
                    System.out.println("0");
                }
                break;
 
            case "front":
                if (q.isEmpty()) {
                    System.out.println(-1);
                } else {
                    System.out.println(q.peek());
                }
                break;
 
            case "back":
                if (q.isEmpty()) {
                    System.out.println(-1);
                } else {
                    System.out.println(last);
                }
                break;
 
            }
 
        }
 
    }
 
}
cs


반응형

+ Recent posts