본문 바로가기

Coding Test/Step19. 조합론

[Java] 1단계. 베라의 패션 [15439번]

728x90

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

  • 예제 입력1
1
  • 예제 출력1
0
  • 예제 입력2
2
  • 예제 출력2
2
  • 예제 입력3
5
  • 예제 출력3
20

  • 문제 접근
    • 입력
      • 첫째 줄 : N (1 <= N <= 2017)
    • 출력
      • 상의와 하의가 다른 서로 다른 색상의 조합 가짓수 출력
  • 문제 해결
    • N * N의 2차원 배열로 나타냄
    • N = 3 이라고 했을 때 i == j가 동일하면 0, 동일하지 않으면 1
    • 배열의 총 합 = 서로 다른 색상의 조합 가짓수
  • 기존 풀이 [메모리 : 30,676 KB / 시간 : 156 ms]
public static void main(String[] args) throws IOException{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int N = Integer.parseInt(br.readLine());
    int[][] combination = new int[N][N];

    for(int i = 0; i < N; i++){
        for(int j = 0; j < N; j++){
            if(i != j) combination[i][j]++;
        }
    }

    int cnt = 0;
    for(int i = 0; i < N; i++){
        cnt += Arrays.stream(combination[i]).sum();
    }

    System.out.println(cnt);
}

 

728x90