<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>ran.log</title>
        <link>https://velog.io/</link>
        <description>Back-End Developer</description>
        <lastBuildDate>Tue, 10 May 2022 16:44:47 GMT</lastBuildDate>
        <docs>https://validator.w3.org/feed/docs/rss2.html</docs>
        <generator>https://github.com/jpmonette/feed</generator>
        <copyright>Copyright (C) 2019. ran.log. All rights reserved.</copyright>
        <atom:link href="https://v2.velog.io/rss/yul_00" rel="self" type="application/rss+xml"/>
        <item>
            <title><![CDATA[[Algorithm/Java] 순열과 조합]]></title>
            <link>https://velog.io/@yul_00/AlgorithmJava-%EC%88%9C%EC%97%B4%EA%B3%BC-%EC%A1%B0%ED%95%A9</link>
            <guid>https://velog.io/@yul_00/AlgorithmJava-%EC%88%9C%EC%97%B4%EA%B3%BC-%EC%A1%B0%ED%95%A9</guid>
            <pubDate>Tue, 10 May 2022 16:44:47 GMT</pubDate>
            <description><![CDATA[<h2 id="순열이란">순열이란?</h2>
<blockquote>
<p>서로 다른 n개 중 r개를 뽑아서 나열한 것</p>
</blockquote>
<h2 id="조합이란">조합이란?</h2>
<blockquote>
<p>서로 다른 n개 중 r개를 <strong>순서 없이</strong> 골라낸 것</p>
</blockquote>
<p>즉, AB != AB와 같이 순서가 의미 있으면 순열이고, 
AB == BA와 같이 순서가 의미 없으면 조합!
그리고 순열, 조합에서 중복을 허용하면 중복 순열, 중복 조합이라고 부른다.
<br/></p>
<p>코드로 구현해보자! (☞ﾟヮﾟ)☞💻</p>
<h2 id="공통-로직">공통 로직</h2>
<pre><code>public class 순열 {
    private static int n, m;
    private static int[] arr; // 원소를 저장할 배열
    private static boolean[] visited; // 중복을 제거하기 위한 방문 처리

    public static void main(String[] args) {
        n = 4;
        m = 2;
        arr = new int[m];
        visited = new boolean[n + 1];
        permutation(0);
    }

}</code></pre><h2 id="순열">순열</h2>
<pre><code>    // 순열
    private static void permutation(int cnt) {
        if (cnt == m) {
            System.out.println(Arrays.toString(arr));
            return;
        }
        for (int i = 1; i &lt;= n; i++) {
            if (!visited[i]) {
                visited[i] = true;
                arr[cnt] = i;
                permutation(cnt + 1);
                visited[i] = false;
            }
        }
    }

    // 중복 순열 - 중복 제거하는 visited를 쓰지 않음
    private static void repeatedPermutation(int cnt) {
        if (cnt == m) {
            System.out.println(Arrays.toString(arr));
            return;
        }
        for (int i = 1; i &lt;= n; i++) {
            arr[cnt] = i;
            permutation(cnt + 1);
        }
    }</code></pre><h3 id="결과">결과</h3>
<p><strong>순열</strong></p>
<pre><code>[1, 2]
[1, 3]
[1, 4]
[2, 1]
[2, 3]
[2, 4]
[3, 1]
[3, 2]
[3, 4]
[4, 1]
[4, 2]
[4, 3]</code></pre><p><strong>중복 순열</strong></p>
<pre><code>[1, 1] - 중복 존재
[1, 2]
[1, 3]
[1, 4]
[2, 1]
[2, 2]
[2, 3]
[2, 4]
[3, 1]
[3, 2]
[3, 3]
[3, 4]
[4, 1]
[4, 2]
[4, 3]
[4, 4]</code></pre><br/>

<h2 id="조합">조합</h2>
<pre><code>    // 조합
    private static void combination(int cnt, int start) {
        if (cnt == m) {
            System.out.println(Arrays.toString(arr));
            return;
        }
        for (int i = start; i &lt;= n; i++) {
            arr[cnt] = i;
            combination(cnt + 1, i + 1); // 오름차순으로 구하면 중복 체크하지 않아도 됨
        }
    }

    // 중복 조합
    private static void repeatedCombination(int cnt, int start) {
        if (cnt == m) {
            System.out.println(Arrays.toString(arr));
            return;
        }
        for (int i = start; i &lt;= n; i++) {
            arr[cnt] = i;
            combination(cnt + 1, i); // 중복 허용하니까 비내림차순
        }
    }</code></pre><h3 id="결과-1">결과</h3>
<p><strong>조합</strong></p>
<pre><code>[1, 2]
[1, 3]
[1, 4]
[2, 3]
[2, 4]
[3, 4]</code></pre><p><strong>중복 조합</strong></p>
<pre><code>[1, 1] - 중복 존재
[1, 2]
[1, 3]
[1, 4]
[2, 2]
[2, 3]
[2, 4]
[3, 3]
[3, 4]
[4, 4]</code></pre><br/>

<h2 id="정리">정리!</h2>
<p>[1, 2] 와 [2, 1]이 다르면 순열!
비유하자면 반에서 반장, 부반장을 뽑는 것과 같다.
[1, 2] 와 [2, 1]이 같으면 조합!
비유하자면 반에서 당번 2명을 뽑는 것과 같다. 두명의 순서가 상관없음</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[[Java] 2차원 배열 회전]]></title>
            <link>https://velog.io/@yul_00/Java-2%EC%B0%A8%EC%9B%90-%EB%B0%B0%EC%97%B4-%ED%9A%8C%EC%A0%84-xf2gjagw</link>
            <guid>https://velog.io/@yul_00/Java-2%EC%B0%A8%EC%9B%90-%EB%B0%B0%EC%97%B4-%ED%9A%8C%EC%A0%84-xf2gjagw</guid>
            <pubDate>Thu, 24 Jun 2021 15:29:34 GMT</pubDate>
            <description><![CDATA[<p>알고리즘을 풀다가 2차원 배열 회전이 나왔는데 까먹지 않기 위해 기록을 해보겠다!</p>
<p><img src="https://images.velog.io/images/yul_00/post/d16fe8f5-38a0-42aa-9529-2863fa265f83/rotate.png" alt="">
그림은 <strong>시계방향(90도)으로 회전하는 것</strong>을 그려보았다.</p>
<p>노란 부분을 보자</p>
<blockquote>
<p>arr2[0][0] = arr1[2][0]
arr2[0][1] = arr1[1][0]
arr2[0][2] = arr1[0][0]</p>
</blockquote>
<p>arr1 배열의 열 값이 arr2 배열의 행 값으로 들어가고
arr2 배열의 행 값은 arr2 배열의 열 값에 반대로 들어가는 것을 볼 수 있다.
<br/></p>
<p>이 규칙을 식으로 만들어보면 요로코롬(●&#39;◡&#39;●)</p>
<blockquote>
<p>arr2[i][j] = arr1[n-1-j][i];</p>
</blockquote>
<br/>

<p>자바 코드로 구현해보자!!</p>
<pre><code>int n = 3; //행의 길이
int m = 4; //열의 길이
int num = 1;
int[][] arr1 = new int[n][m];
for (int i = 0; i &lt; n; i++) {
    for (int j = 0; j &lt; m; j++) {
        arr1[i][j] = num++;
    }
}

int[][] arr2 = new int[m][n]; // 행, 열을 반대로
for (int i = 0; i &lt; m; i++) { 
    for (int j = 0; j &lt; n; j++) {
        arr2[i][j] = arr1[n - 1 - j][i]; // ##핵심 코드##
    }
}</code></pre><p>출력해서 결과를 보면</p>
<blockquote>
<p>--arr1--
1 2 3 4 
5 6 7 8 
9 10 11 12 
--arr2--
9 5 1 
10 6 2 
11 7 3 
12 8 4 </p>
</blockquote>
<p>시계방향으로 회전이 되었다!!</p>
<p><br/><br/>
하는 김에 <strong>반시계 방향</strong>도 해보자</p>
<blockquote>
<p>arr2[i][j] = arr1[j][m-1-i];</p>
</blockquote>
<p>코드</p>
<pre><code>int n = 3;
int m = 4;
int[][] arr1 = new int[n][m];
int num = 1;
for (int i = 0; i &lt; n; i++) {
    for (int j = 0; j &lt; m; j++) {
        arr1[i][j] = num++;
    }
}

int[][] arr2 = new int[m][n];
for (int i = 0; i &lt; m; i++) {
    for (int j = 0; j &lt; n; j++) {
        arr2[i][j] = arr1[j][m - 1 - i]; // ##핵심 코드##
    }
}</code></pre><p>결과</p>
<blockquote>
<p>--arr1--
1 2 3 4 
5 6 7 8 
9 10 11 12 
--arr2--
4 8 12 
3 7 11 
2 6 10 
1 5 9 </p>
</blockquote>
]]></description>
        </item>
        <item>
            <title><![CDATA[Arrays.fill 사용 시 ArrayStoreException]]></title>
            <link>https://velog.io/@yul_00/Arrays.fill-%EC%82%AC%EC%9A%A9-%EC%8B%9C-ArrayStoreException</link>
            <guid>https://velog.io/@yul_00/Arrays.fill-%EC%82%AC%EC%9A%A9-%EC%8B%9C-ArrayStoreException</guid>
            <pubDate>Tue, 08 Jun 2021 09:28:54 GMT</pubDate>
            <description><![CDATA[<pre><code>int[][] a = new int[n][n];
Arrays.fill(a, -1);</code></pre><p>알고리즘을 풀다가 2차원 배열을 특정 값으로 초기화하는데 </p>
<blockquote>
<p>Exception in thread &quot;main&quot; java.lang.ArrayStoreException: java.lang.Integer</p>
</blockquote>
<p>이런 에러가 떴다!
<br/></p>
<p>찾아보니 Arrays.fill은 1차원 배열에서만 사용할 수 있다고 한다.</p>
<pre><code>for (int[] i : a) Arrays.fill(i, -1);</code></pre><p>for문을 사용하여 해결하였다.</p>
]]></description>
        </item>
    </channel>
</rss>