<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>hyrax.log</title>
        <link>https://velog.io/</link>
        <description></description>
        <lastBuildDate>Thu, 09 Feb 2023 05:37:14 GMT</lastBuildDate>
        <docs>https://validator.w3.org/feed/docs/rss2.html</docs>
        <generator>https://github.com/jpmonette/feed</generator>
        <image>
            <title>hyrax.log</title>
            <url>https://velog.velcdn.com/images/h2_30r1d/profile/b358b909-f848-41f5-94c6-95268c36801e/image.jpg</url>
            <link>https://velog.io/</link>
        </image>
        <copyright>Copyright (C) 2019. hyrax.log. All rights reserved.</copyright>
        <atom:link href="https://v2.velog.io/rss/h2_30r1d" rel="self" type="application/rss+xml"/>
        <item>
            <title><![CDATA[[JAVA] 백준 1463: 1로 만들기]]></title>
            <link>https://velog.io/@h2_30r1d/%EB%B0%B1%EC%A4%80</link>
            <guid>https://velog.io/@h2_30r1d/%EB%B0%B1%EC%A4%80</guid>
            <pubDate>Thu, 09 Feb 2023 05:37:14 GMT</pubDate>
            <description><![CDATA[<h3 id="문제">문제</h3>
<p><a href="https://www.acmicpc.net/problem/1463">https://www.acmicpc.net/problem/1463</a></p>
<hr>
<blockquote>
<p>[Do it! 알고리즘 코딩테스트 자바편]</p>
</blockquote>
<h3 id="과정">과정</h3>
<p><strong>1. 문제 분석 &amp; 풀어보기</strong></p>
<p>dp[i]: i에서 1로 만드는 데 걸리는 최소 연산 횟수</p>
<pre><code>dp[i] = dp[i - 1] + 1
if(i % 2 == 0) dp[i] = min(dp[i], dp[i / 2] + 1)
if(i % 3 == 0) dp[i] = min(dp[i], dp[i / 3] + 1)</code></pre><p><strong>3. 코드</strong></p>
<pre><code class="language-java">package Baekjoon;

import java.io.IOException;
import java.util.Scanner;

public class S3_1463 {

  public static void main(String[] args) throws IOException {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[] dp = new int[n + 1];
    //dp[x] = x에서 1로 만드는 최소 횟수

    dp[1] = 0;

    for (int i = 2; i &lt;= n; i++) {
      dp[i] = dp[i - 1] + 1;
      if(i % 2 == 0) dp[i] = Math.min(dp[i], dp[i / 2] + 1);
      if(i % 3 == 0) dp[i] = Math.min(dp[i], dp[i / 3] + 1);
    }

    System.out.println(dp[n]);

  }//main
}//class</code></pre>
<hr>
<pre><code class="language-java">
package Baekjoon;

import java.io.IOException;
import java.util.Scanner;

public class S3_1463 {

  public static void main(String[] args) throws IOException {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[] dp = new int[n + 1];
    //dp[x] = x에서 1로 만드는 최소 횟수

    dp[2] = 1;
    dp[3] = 1;

    for (int i = 4; i &lt;= n; i++) {
      dp[i] = dp[i - 1] + 1;
      if(i % 2 == 0) dp[i] = Math.min(dp[i], dp[i / 2] + 1);
      if(i % 3 == 0) dp[i] = Math.min(dp[i], dp[i / 3] + 1);
    }

    System.out.println(dp[n]);

  }//main
}//class</code></pre>
<p>처음에는 이렇게 짰는데, n이 4 이하면 94%에서 index Error가 나서 실패한다! </p>
]]></description>
        </item>
        <item>
            <title><![CDATA[[JAVA] 백준 11724: 연결 요소의 개수]]></title>
            <link>https://velog.io/@h2_30r1d/JAVA-%EB%B0%B1%EC%A4%80-11724-%EC%97%B0%EA%B2%B0-%EC%9A%94%EC%86%8C%EC%9D%98-%EA%B0%9C%EC%88%98</link>
            <guid>https://velog.io/@h2_30r1d/JAVA-%EB%B0%B1%EC%A4%80-11724-%EC%97%B0%EA%B2%B0-%EC%9A%94%EC%86%8C%EC%9D%98-%EA%B0%9C%EC%88%98</guid>
            <pubDate>Tue, 10 Jan 2023 05:22:36 GMT</pubDate>
            <description><![CDATA[<h3 id="문제">문제</h3>
<p>실버 2
<a href="https://www.acmicpc.net/problem/11724">https://www.acmicpc.net/problem/11724</a></p>
<h3 id="풀이">풀이</h3>
<pre><code class="language-java">package Baekjoon;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class S2_11724 {
  static ArrayList&lt;Integer&gt;[] close;
  static boolean[] visited;

  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer(br.readLine());

    int n = Integer.parseInt(st.nextToken());
    int m = Integer.parseInt(st.nextToken());

    close = new ArrayList[n + 1];
    visited = new boolean[n + 1];

    for (int i = 1; i &lt; n + 1; i++) {
      close[i] = new ArrayList&lt;&gt;();
    }

    for (int i = 0; i &lt; m; i++) {
      st = new StringTokenizer(br.readLine());
      int u = Integer.parseInt(st.nextToken());
      int v = Integer.parseInt(st.nextToken());

      close[u].add(v);
      close[v].add(u);
    }

    int cnt = 0;
    for (int i = 1; i &lt; n + 1; i++) {
      if (!visited[i]) {
        cnt++;
        DFS(i);
      }
    }
    System.out.println(cnt);
  }

    static void DFS(int d) {
      if (visited[d]) {
        return;
      }
      visited[d] = true;
      for (int i : close[d]) {
        if (!visited[i]) {
          DFS(i);
        }
      }
    }
  }
</code></pre>
]]></description>
        </item>
        <item>
            <title><![CDATA[[JAVA] 백준 12891: DNA 비밀번호]]></title>
            <link>https://velog.io/@h2_30r1d/JAVA-%EB%B0%B1%EC%A4%80-12891-DNA-%EB%B9%84%EB%B0%80%EB%B2%88%ED%98%B8</link>
            <guid>https://velog.io/@h2_30r1d/JAVA-%EB%B0%B1%EC%A4%80-12891-DNA-%EB%B9%84%EB%B0%80%EB%B2%88%ED%98%B8</guid>
            <pubDate>Thu, 05 Jan 2023 04:45:47 GMT</pubDate>
            <description><![CDATA[<h3 id="문제">문제</h3>
<p>실버 2
<a href="https://www.acmicpc.net/problem/12891">https://www.acmicpc.net/problem/12891</a></p>
<h3 id="풀이">풀이</h3>
<pre><code class="language-java">package Baekjoon;

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

public class S2_12891 {

  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer(br.readLine());

    //s, p, ans
    int s = Integer.parseInt(st.nextToken());
    int p = Integer.parseInt(st.nextToken());
    int ans = 0;

    //dna
    st = new StringTokenizer(br.readLine());
    char[] dna = st.nextToken().toCharArray();

    //acgt 카운트
    int[] acgt = new int[4];
    st = new StringTokenizer(br.readLine());
    for (int i = 0; i &lt; 4; i++) {
      acgt[i] = Integer.parseInt(st.nextToken());
    }

    //첫번째 부분 문자열
    int start = 0;
    int end = p - 1;
    char[] first_arr = Arrays.copyOfRange(dna, start, end+1);

//    System.out.println(first_arr);

    //부분문자열의 acgt 개수 배열
    int[] cnt = new int[4];

    for (char f : first_arr) {
      count(f, cnt, 1);
    }

    if (cnt[0] &gt;= acgt[0] &amp;&amp; cnt[1] &gt;= acgt[1] &amp;&amp; cnt[2] &gt;= acgt[2] &amp;&amp; cnt[3] &gt;= acgt[3]) {
      ans++;
    }

//    System.out.println(Arrays.toString(cnt));

    while (end + 1 &lt; s) {
      count(dna[start], cnt, -1);
      start++;
      end++;
      //처음과 끝 카운트
      count(dna[end], cnt, 1);
//      System.out.println(dna[start] + &quot; &quot; + dna[end]);
//      System.out.println(Arrays.toString(cnt));

      if (cnt[0] &gt;= acgt[0] &amp;&amp; cnt[1] &gt;= acgt[1] &amp;&amp; cnt[2] &gt;= acgt[2] &amp;&amp; cnt[3] &gt;= acgt[3]) {
        ans++;

      }
    }

    System.out.println(ans);

  }

  static void count(char c, int[] cnt_arr, int add) {
    if (c == &#39;A&#39;) {
      cnt_arr[0] += add;
    } else if (c == &#39;C&#39;) {
      cnt_arr[1] += add;
    } else if (c == &#39;G&#39;) {
      cnt_arr[2] += add;
    } else if (c == &#39;T&#39;) {
      cnt_arr[3] += add;
    }
  }
}</code></pre>
<hr>
<h3 id="다른-풀이">다른 풀이</h3>
<pre><code class="language-java">package Baekjoon;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;

public class dfhjkd {

  static int[] cnt= new int[&#39;Z&#39;+1];


  public static void main(String[] args) throws IOException {
    BufferedReader input= new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer tokens = new StringTokenizer(input.readLine());

    int S=Integer.parseInt(tokens.nextToken());
    int P=Integer.parseInt(tokens.nextToken());

    String Dna = input.readLine();

    int[] min_num=new int[4];

    int totalCnt = 0;

    tokens= new StringTokenizer(input.readLine());
    for(int i=0;i&lt;4;i++) {
      min_num[i]=Integer.parseInt(tokens.nextToken());
    }

    for(int i=0;i&lt;P;i++) {
      cnt[Dna.charAt(i)]++;
    }

    if(isAble(min_num)) {
      totalCnt++;
    }

    for(int i=0;i&lt;S-P;i++) {
      cnt[Dna.charAt(i)]--;
      cnt[Dna.charAt(i+P)]++;

      if(isAble(min_num)) totalCnt++;

    }

    System.out.println(totalCnt);

  }

  static boolean isAble(int[] min_num) {
    return cnt[&#39;A&#39;]&gt;=min_num[0] &amp;&amp; cnt[&#39;C&#39;]&gt;=min_num[1] &amp;&amp; cnt[&#39;G&#39;]&gt;=min_num[2] &amp;&amp; cnt[&#39;T&#39;]&gt;=min_num[3];
  }


}
</code></pre>
<p>이런 방법이.,..</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[[JAVA] 백준 2018: 수들의 합 5]]></title>
            <link>https://velog.io/@h2_30r1d/JAVA-%EB%B0%B1%EC%A4%80-2018-%EC%88%98%EB%93%A4%EC%9D%98-%ED%95%A9-5</link>
            <guid>https://velog.io/@h2_30r1d/JAVA-%EB%B0%B1%EC%A4%80-2018-%EC%88%98%EB%93%A4%EC%9D%98-%ED%95%A9-5</guid>
            <pubDate>Tue, 03 Jan 2023 13:28:15 GMT</pubDate>
            <description><![CDATA[<h3 id="문제">문제</h3>
<p>실버 5
<a href="https://www.acmicpc.net/problem/2018">https://www.acmicpc.net/problem/2018</a></p>
<h3 id="풀이">풀이</h3>
<pre><code class="language-java">package Baekjoon;

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

public class S5_2018 {

  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer(br.readLine());

    int n = Integer.parseInt(st.nextToken());
    int start = 1;
    int end = 2;
    int sum;
    int answer = 1;

    while (start &lt; n / 2 + 1) {
      sum = (start + end) * (end - start + 1) / 2;

      if (sum &lt; n) {
        end++;
      }
      else if (sum == n) {
        answer++;
        start++;
        end = start + 1;
      }
      else {
        start++;
        end = start + 1;
      }
    }

    System.out.println(answer);
  }

}
</code></pre>
<h4 id="등차수열의-합">등차수열의 합</h4>
<pre><code>(start + end) * (end - start + 1) / 2</code></pre><hr>
<blockquote>
<p>[Do it! 알고리즘 코딩테스트 자바편]</p>
</blockquote>
<h3 id="과정">과정</h3>
<p><strong>1. 문제 분석 &amp; 풀어보기</strong></p>
<ul>
<li>나타내는 방법에 숫자 그대로도 있기 때문에 count를 1로 초기화</li>
<li>start_index를 오른쪽으로 한 칸 이동 == 연속된 자연수에서 왼쪽 값 삭제</li>
<li>end_index를 오른쪽으로 한 칸 이동 == 연속된 자연수의 범위 한 칸 더 확장</li>
</ul>
<p><strong>2. 슈도코드</strong>
<img src="https://velog.velcdn.com/images/h2_30r1d/post/263a4cb4-d4c9-4f63-992c-44839354a2b2/image.png" alt=""></p>
<p><strong>3. 코드</strong></p>
<pre><code class="language-java">    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int count = 1;
    int start_index = 1;
    int end_index = 1;
    int sum = 1;

    while (end_index != n) {
      if (sum == n) {
        count++;
        end_index++;
        sum += end_index;
      } else if (sum &gt; n) {
        sum -= start_index;
        start_index++;
      } else {
        end_index++;
        sum += end_index;
      }
    }
    System.out.println(count);</code></pre>
]]></description>
        </item>
        <item>
            <title><![CDATA[[JAVA] 백준 10986: 나머지 합]]></title>
            <link>https://velog.io/@h2_30r1d/JAVA-%EB%B0%B1%EC%A4%80-10986-%EB%82%98%EB%A8%B8%EC%A7%80-%ED%95%A9</link>
            <guid>https://velog.io/@h2_30r1d/JAVA-%EB%B0%B1%EC%A4%80-10986-%EB%82%98%EB%A8%B8%EC%A7%80-%ED%95%A9</guid>
            <pubDate>Tue, 03 Jan 2023 11:48:12 GMT</pubDate>
            <description><![CDATA[<h3 id="문제">문제</h3>
<p>골드 3
<a href="https://www.acmicpc.net/problem/10986">https://www.acmicpc.net/problem/10986</a></p>
<h3 id="풀이">풀이</h3>
<pre><code class="language-java">package Baekjoon;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
//import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
import java.util.stream.Collectors;

public class G3_10986 {

  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer(br.readLine());

    //n, m, ans
    int n = Integer.parseInt(st.nextToken()), m = Integer.parseInt(st.nextToken());
    long ans = 0;

    //n개의 수 배열
    ArrayList&lt;Integer&gt; input = new ArrayList&lt;&gt;();

    st = new StringTokenizer(br.readLine());
    for (int i = 0; i &lt; n; i++) {
      input.add(Integer.parseInt(st.nextToken()));
    }

    //누적합 배열
    long[] sum = new long[n+1];
    for (int i = 1; i &lt;= n; i++) {
      sum[i] = sum[i - 1] + input.get(i-1);
    }

    //나머지 배열
    ArrayList&lt;Long&gt; rest = new ArrayList&lt;&gt;();
    for (int i = 1; i &lt;= n; i++) {
      rest.add(sum[i] % m);

    }

    //나머지 배열에 0이 있을 경우 개수 1 더하기
    for (long r: rest) {
      if (r == 0) {
        ans += 1;
      }
    }

    //나머지 배열에서 개수 구하기
    //나머지 배열 중복제거
    List&lt;Long&gt; done = rest.stream().distinct().collect(Collectors.toList());
    for (long d : done) {
      long cnt = Collections.frequency(rest, d);  //배열에서 개수 구하기
      ans += (cnt * (cnt - 1) / 2);
    }
    System.out.println(ans);
  }
}
</code></pre>
<p>누적합을 구한 이유
-&gt; 부분합을 구하기 위해서
-&gt; 누적합에서 부분합 구하는 방법은 S[j]-S[i-1]</p>
<p>두 수를 각각 m으로 나눴을때의 나머지가 같으면
두 수를 뺀 값도 나머지가 0일 것이다</p>
<p>누적합의 나머지를 구해보면
1 0 0 1 0</p>
<p>0이면 나누어떨어지는 거라서 답에 더해주기</p>
<p>0 세개 -&gt; 두 개 아무거나 골라서 서로 빼더라도 0이라는 거니까 -&gt; 3C2
1로 똑같은거 두개는 2C2</p>
<p>mCn일 때 공식이
m*(m-1) // 2
n이 2로 고정
-&gt; 부분합 구할때 두개의 인덱스만 필요하기 때문에</p>
<hr>
<blockquote>
<p>[Do it! 알고리즘 코딩테스트 자바편]</p>
</blockquote>
<h3 id="과정">과정</h3>
<p><strong>1. 문제 분석 &amp; 풀어보기</strong>
(A+B) % C 는 ((A%C) + (B%C)) % C랑 같다.</p>
<p><strong>2. 슈도코드</strong>
<img src="https://velog.velcdn.com/images/h2_30r1d/post/0f1d3448-de5d-4873-b4a8-95bb8bf3141c/image.png" alt=""></p>
<p><strong>3. 코드</strong></p>
<pre><code class="language-java">    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int m = sc.nextInt();
    long[] S = new long[n];
    long[] C = new long[m];
    long answer = 0;
    //바로 입력받아서 합 배열 만들기
    S[0] = sc.nextInt();
    for (int i = 1; i &lt;= n; i++) {
      S[i] = S[i - 1] + sc.nextInt();
    }
    for (int i = 0; i &lt; n; i++) {
      int remainder = (int) (S[i] % m);
      //구간합 자체가 0일때 정답에 더하기
      if (remainder == 0) answer++;
      //나머지에 해당하는 인덱스 개수 카운팅
      C[remainder]++;
    }
    for (int i = 0; i &lt; m; i++) {
      if (C[i] &gt; 1) {
        answer += C[i] * (C[i] - 1) / 2;
      }
    }
    System.out.println(answer);</code></pre>
<p>나머지가 같은 인덱스의 개수 카운팅하는 방법</p>
<hr>
<h3 id="추가">추가</h3>
<p>계산 과정에서 int형이 저장할 수 없는 범위의 값이 나오면 틀린 결과가 나올 수 있다. long형으로 선언하면 오류가 발생하지 않는다.</p>
<hr>
<h3 id="여담">여담</h3>
<p>아이디어 떠올리기도 어려웠는데 그 와중에 계속 int로 해서 계속 틀림
ㅎㅎㅎ ㅎ ㅎㅎ    ㅎ</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[[JAVA] 백준: 배열/리스트 뽀개기]]></title>
            <link>https://velog.io/@h2_30r1d/JAVA-%EB%B0%B1%EC%A4%80-%EB%B0%B0%EC%97%B4-%EB%BD%80%EA%B0%9C%EA%B8%B0</link>
            <guid>https://velog.io/@h2_30r1d/JAVA-%EB%B0%B1%EC%A4%80-%EB%B0%B0%EC%97%B4-%EB%BD%80%EA%B0%9C%EA%B8%B0</guid>
            <pubDate>Fri, 30 Dec 2022 17:33:27 GMT</pubDate>
            <description><![CDATA[<h2 id="난이도별-문제-정리">난이도별 문제 정리</h2>
<p>스탠다드
<a href="https://velog.io/@h2_30r1d/JAVA-%EB%B0%B1%EC%A4%80-10807-%EA%B0%9C%EC%88%98-%EC%84%B8%EA%B8%B0">백준 10807: 개수 세기</a></p>
<hr>
<p>브론즈
<a href="https://velog.io/@h2_30r1d/JAVA-%EB%B0%B1%EC%A4%80-2562">3 - 백준 2562: 최댓값</a></p>
<p><a href="https://velog.io/@h2_30r1d/JAVA-%EB%B0%B1%EC%A4%80-11720-%EC%88%AB%EC%9E%90%EC%9D%98-%ED%95%A9">2 - 백준 11720: 숫자의 합</a></p>
<p><a href="https://velog.io/@h2_30r1d/JAVA-%EB%B0%B1%EC%A4%80-1546-%ED%8F%89%EA%B7%A0">1 - 백준 1546: 평균</a></p>
<p><a href="https://velog.io/@h2_30r1d/B113322">1 - 백준 13322: 접두사 배열</a></p>
<hr>
<p>실버
<a href="https://velog.io/@h2_30r1d/S5-2167">5 - 백준 2167: 2차원 배열의 합</a></p>
<p><a href="https://velog.io/@h2_30r1d/s511728">5 - 백준 11728: 배열 합치기</a></p>
<p><a href="https://velog.io/@h2_30r1d/25966">5 - 백준 25966: 배찬우는 배열을 좋아해</a></p>
<p><a href="https://velog.io/@h2_30r1d/S411656">4 - 백준 11656: 접미사 배열</a></p>
<p><a href="https://velog.io/@h2_30r1d/S4">4 - 백준 1337: 올바른 배열</a></p>
<p><a href="https://velog.io/@h2_30r1d/JAVA-%EB%B0%B1%EC%A4%80-11659-%EA%B5%AC%EA%B0%84-%ED%95%A9-%EA%B5%AC%ED%95%98%EA%B8%B0-4">3 - 백준 11659: 구간 합 구하기 4</a></p>
<p><a href="https://velog.io/@h2_30r1d/JAVA-%EB%B0%B1%EC%A4%80-11660-%EA%B5%AC%EA%B0%84-%ED%95%A9-%EA%B5%AC%ED%95%98%EA%B8%B0-5">1 - 백준 11660: 구간 합 구하기 5</a></p>
]]></description>
        </item>
        <item>
            <title><![CDATA[[JAVA] 백준 11660: 구간 합 구하기 5]]></title>
            <link>https://velog.io/@h2_30r1d/JAVA-%EB%B0%B1%EC%A4%80-11660-%EA%B5%AC%EA%B0%84-%ED%95%A9-%EA%B5%AC%ED%95%98%EA%B8%B0-5</link>
            <guid>https://velog.io/@h2_30r1d/JAVA-%EB%B0%B1%EC%A4%80-11660-%EA%B5%AC%EA%B0%84-%ED%95%A9-%EA%B5%AC%ED%95%98%EA%B8%B0-5</guid>
            <pubDate>Fri, 30 Dec 2022 17:18:47 GMT</pubDate>
            <description><![CDATA[<h3 id="문제">문제</h3>
<p><a href="https://www.acmicpc.net/problem/11660">https://www.acmicpc.net/problem/11660</a></p>
<h3 id="풀이">풀이</h3>
<pre><code class="language-java">package Baekjoon;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class S1_11660 {
  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer(br.readLine());
    StringBuilder sb = new StringBuilder();

    //n, m 입력
    int n = Integer.parseInt(st.nextToken());
    int m = Integer.parseInt(st.nextToken());

    ArrayList&lt;int[]&gt; sumList = new ArrayList&lt;&gt;();
    int[] sum;

    //행렬 입력
    int[][] matrix = new int[n][n];

    for (int i = 0; i &lt; n; i++) {
      st = new StringTokenizer(br.readLine());
      for (int j = 0; j &lt; n; j++) {
        matrix[i][j] = Integer.parseInt(st.nextToken());
      }
    }

    for (int[] arr : matrix) {
      //합배열 만들기
      sum = new int[n+1];
      for (int i = 1; i &lt;= n; i++) {
        sum[i] = sum[i-1] + arr[i-1];
      }
      sumList.add(sum);
    }

    //구해야하는 합배열
    for (int i = 0; i &lt; m; i++) {
      st = new StringTokenizer(br.readLine());
      int x1 = Integer.parseInt(st.nextToken());
      int y1 = Integer.parseInt(st.nextToken());
      int x2 = Integer.parseInt(st.nextToken());
      int y2 = Integer.parseInt(st.nextToken());
      int result = 0;

      for (int x = x1-1; x &lt;= x2-1; x++) {
        int[] rowSum = sumList.get(x);
        result += rowSum[y2] - rowSum[y1-1];
      }
      sb.append(result + &quot;\n&quot;);
    }
    System.out.print(sb);

  }
}
</code></pre>
<p>시간 초과가 떠서 아예 알고리즘을 수정했다.
2차원 배열에서 행 별로 합 배열을 만들고, 입력 인덱스만큼의 값만 누적해서 result에 더하고 출력했다.</p>
<hr>
<h3 id="시간-초과-풀이">시간 초과 풀이</h3>
<pre><code class="language-java">import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer(br.readLine());
    StringBuilder sb = new StringBuilder();

    //n, m 입력
    int n = Integer.parseInt(st.nextToken());
    int m = Integer.parseInt(st.nextToken());

    //행렬 입력
    String[][] matrix = new String[n][n];

    for (int i = 0; i &lt; n; i++) {
      matrix[i] = br.readLine().split(&quot; &quot;);
    }

    //질의 입력
    for (int i = 0; i &lt; m; i++) {
      st = new StringTokenizer(br.readLine());
      int x1 = Integer.parseInt(st.nextToken()) - 1;
      int y1 = Integer.parseInt(st.nextToken()) - 1;
      int x2 = Integer.parseInt(st.nextToken()) - 1;
      int y2 = Integer.parseInt(st.nextToken()) - 1;
      int sum = 0;

      for (int x = x1; x &lt;= x2; x++) {
        for (int y = y1; y &lt;= y2; y++) {
          sum += Integer.parseInt(matrix[x][y]);
        }
      }
      sb.append(sum + &quot;\n&quot;);
    }
    System.out.print(sb);
  }
}

</code></pre>
<hr>
<blockquote>
<p>[Do it! 알고리즘 코딩테스트 자바편]</p>
</blockquote>
<h3 id="과정">과정</h3>
<p><strong>1. 문제 분석 &amp; 풀어보기</strong>
질의 개수가 100,000이므로 질의마다 합을 구하면 안되고, 구간 합 배열을 이용해야 한다.</p>
<p><strong>2차원 구간 합 배열 D[X][Y] 정의</strong></p>
<pre><code>D[X][Y] = 원본 배열의 (0,0)부터 (X,Y)까지의 사각형 영역 안에 있는 수의 합</code></pre><p><strong>D[i][j]의 값을 채우는 구간 합 공식</strong></p>
<pre><code>D[i][j] = D[i][j-1] + D[i-1][j] - D[i-1][j-1] + A[i][j]</code></pre><p><strong>질의 X1, Y1, X2, Y2에 대한 답을 구간 합으로 구하는 방법</strong></p>
<pre><code>D[X2][Y2] - D[X1-1][Y2] - D[X2][Y1-1] + D[X1-1][Y1-1]</code></pre><p><strong>2. 슈도코드</strong>
<img src="https://velog.velcdn.com/images/h2_30r1d/post/f65cfac3-9091-438a-b17b-d1faecc9ee50/image.png" alt=""></p>
<p><strong>3. 코드</strong></p>
<pre><code class="language-java">public class S1_11660 {
  public static void main(String[] args) throws IOException {

      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer(br.readLine());

    int N = Integer.parseInt(st.nextToken());
    int M = Integer.parseInt(st.nextToken());

    int A[][] = new int[N+1][N+1];
    for (int i = 0; i &lt; N; i++) {
      st = new StringTokenizer(br.readLine());
      for (int j = 0; j &lt; N; j++) {
        A[i][j] = Integer.parseInt(st.nextToken());
      }
    }

    int D[][] = new int[N+1][N+1];
    for (int i = 0; i &lt; N; i++) {
      for (int j = 0; j &lt; N; j++) {
        D[i][j] = D[i][j-1] + D[i-1][j] - D[i-1][j-1] + A[i][j];
      }
    }

    for (int i = 0; i &lt; m; i++) {
      st = new StringTokenizer(br.readLine());
      int x1 = Integer.parseInt(st.nextToken());
      int y1 = Integer.parseInt(st.nextToken());
      int x2 = Integer.parseInt(st.nextToken());
      int y2 = Integer.parseInt(st.nextToken());

      int result = D[x2][y2] - D[x1-1][y2] - D[x2][y1-1] + D[x1-1][y1-1]
      System.out.println(result);

      }
  }
}
</code></pre>
<p>사실 공식 부분 제대로 이해못했다. ㅎ헤헤</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[[JAVA] 백준 1337: 올바른 배열]]></title>
            <link>https://velog.io/@h2_30r1d/S4</link>
            <guid>https://velog.io/@h2_30r1d/S4</guid>
            <pubDate>Fri, 30 Dec 2022 16:50:30 GMT</pubDate>
            <description><![CDATA[<h3 id="문제">문제</h3>
<p>실버 4
<a href="https://www.acmicpc.net/problem/1337">https://www.acmicpc.net/problem/1337</a></p>
<h3 id="풀이">풀이</h3>
<pre><code class="language-java">package Baekjoon;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;

public class S4_1337 {

  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    int n = Integer.parseInt(br.readLine());
    ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();

    for (int i = 0; i &lt; n; i++) {
      list.add(Integer.parseInt(br.readLine()));
    }

    Collections.sort(list);

    int[] cnt = new int[n];

    for (int i = 0; i &lt; n; i++) {
      for (int j = 1; j &lt; 5; j++) {
        if (!list.contains(list.get(i) + j)) {
          cnt[i]++;
        };
      }
    }

// 정렬해서 맨처음 값 출력하기
//    Arrays.sort(cnt);
//    System.out.println(cnt[0]);

    int min = cnt[0];
    for (int c : cnt) {
      if (c &lt;= min) {
        min = c;
      }
    }
    System.out.println(min);
  }
}
</code></pre>
<p>배열의 각 요소마다 1, 2, 3, 4, 5를 더한 값이 입력 리스트에 있는지 비교했다. 없는만큼 개수를 1 더했고, 최종적으로 개수 리스트의 최소값을 출력했다.</p>
<hr>
<h3 id="여담">여담</h3>
<p>효율 생각해서 어렵게 접근하니까 잘 안풀렸다...
감 잡는데 좀 오래걸림...</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[[JAVA] 백준 11656: 접미사 배열]]></title>
            <link>https://velog.io/@h2_30r1d/S411656</link>
            <guid>https://velog.io/@h2_30r1d/S411656</guid>
            <pubDate>Fri, 30 Dec 2022 16:36:31 GMT</pubDate>
            <description><![CDATA[<h3 id="문제">문제</h3>
<p>실버 4
<a href="https://www.acmicpc.net/problem/11656">https://www.acmicpc.net/problem/11656</a></p>
<h3 id="풀이">풀이</h3>
<pre><code class="language-java">package Baekjoon;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;

public class S4_11656 {

  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String str = br.readLine();
    ArrayList&lt;String&gt; strList = new ArrayList&lt;&gt;();
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i &lt; str.length(); i++) {
      strList.add(str.substring(i));
    }
    Collections.sort(strList);

    for (String s : strList) {
      sb.append(s + &quot;\n&quot;);
    }
    System.out.print(sb);
  }
}
</code></pre>
<p>substring 이용하니까 쉽다!</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[[JAVA] 백준 25966: 배찬우는 배열을 좋아해]]></title>
            <link>https://velog.io/@h2_30r1d/25966</link>
            <guid>https://velog.io/@h2_30r1d/25966</guid>
            <pubDate>Fri, 30 Dec 2022 16:33:24 GMT</pubDate>
            <description><![CDATA[<h3 id="문제">문제</h3>
<p>실버 5
<a href="https://www.acmicpc.net/problem/25966">https://www.acmicpc.net/problem/25966</a></p>
<h3 id="풀이">풀이</h3>
<pre><code class="language-java">package Baekjoon;

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

public class S5_25966 {
  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    //개수 입력
    StringTokenizer st = new StringTokenizer(br.readLine());
    StringBuilder sb = new StringBuilder();
    int n = Integer.parseInt(st.nextToken()), m = Integer.parseInt(st.nextToken()), q = Integer.parseInt(st.nextToken());

    //행렬 입력
    int[][] matrix = new int[n][m];
    for (int i = 0; i &lt; n; i++) {
      st = new StringTokenizer(br.readLine());
      for (int j = 0; j &lt; m; j++) {
        matrix[i][j] = Integer.parseInt(st.nextToken());
      }
    }

    //쿼리 입력
    for (int c = 0; c &lt; q; c++) {
      st = new StringTokenizer(br.readLine());
      int first = Integer.parseInt(st.nextToken());
      int i = Integer.parseInt(st.nextToken());
      int j = Integer.parseInt(st.nextToken());

      if (first == 0) {
        int k = Integer.parseInt(st.nextToken());
        matrix[i][j] = k;
      }
      else {
        int[] tmp = matrix[i];
        matrix[i] = matrix[j];
        matrix[j] = tmp;
      }
    }
    //출력
    for (int i = 0; i &lt; n; i++) {
      for (int j = 0; j &lt; m; j++) {
        sb.append(matrix[i][j] + &quot; &quot;);
      }
      sb.append(&quot;\n&quot;);
    }
    System.out.print(sb);
  }
}</code></pre>
<hr>
<h3 id="여담">여담</h3>
<p>배찬우씨...
계속계쏙계속 시간 초과가 떠서 열받았다...
StringBuilder의 중요성을 깨달았따....</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[[JAVA] 백준 11728: 배열 합치기]]></title>
            <link>https://velog.io/@h2_30r1d/s511728</link>
            <guid>https://velog.io/@h2_30r1d/s511728</guid>
            <pubDate>Fri, 30 Dec 2022 16:30:44 GMT</pubDate>
            <description><![CDATA[<h3 id="문제">문제</h3>
<p>실버 5
<a href="https://www.acmicpc.net/problem/11728">https://www.acmicpc.net/problem/11728</a></p>
<h3 id="풀이">풀이</h3>
<pre><code class="language-java">package Baekjoon;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;

public class S5_11728 {

  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

    StringTokenizer st = new StringTokenizer(br.readLine());
    int n = Integer.parseInt(st.nextToken());
    int m = Integer.parseInt(st.nextToken());
    ArrayList&lt;Integer&gt; list = new ArrayList&lt;&gt;();

    st = new StringTokenizer(br.readLine());
    for (int i = 0; i &lt; n; i++) {
      list.add(Integer.parseInt(st.nextToken()));
    }

    st = new StringTokenizer(br.readLine());
    for (int i = 0; i &lt; m; i++) {
      list.add(Integer.parseInt(st.nextToken()));
    }
    Collections.sort(list);

    for (int l : list) {
      bw.write(l + &quot; &quot;);
    }
    bw.flush();
    bw.close();
    br.close();
  }
}
</code></pre>
<p>각각 입력받아서 하나의 ArrayList에 바로 추가했다. 배열 크기가 1,000,000개까지 가능해서 BufferedWriter로 처리했다. bw.flush와 bw.close는 반드시 해줘야 한다!</p>
<hr>
<h3 id="추가">추가</h3>
<p>BufferedReader, BufferedWriter를 활용한 빠른 입출력
<a href="https://coding-factory.tistory.com/251">https://coding-factory.tistory.com/251</a></p>
]]></description>
        </item>
        <item>
            <title><![CDATA[[JAVA] 백준 2167: 2차원 배열의 합]]></title>
            <link>https://velog.io/@h2_30r1d/S5-2167</link>
            <guid>https://velog.io/@h2_30r1d/S5-2167</guid>
            <pubDate>Fri, 30 Dec 2022 16:15:55 GMT</pubDate>
            <description><![CDATA[<h3 id="문제">문제</h3>
<p>실버 5
<a href="https://www.acmicpc.net/problem/2167">https://www.acmicpc.net/problem/2167</a></p>
<h3 id="풀이">풀이</h3>
<pre><code class="language-java">package Baekjoon;

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

public class S5_2167 {
  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer(br.readLine());

    int n = Integer.parseInt(st.nextToken());
    int m = Integer.parseInt(st.nextToken());

    // 2차원 배열 생성
    int[][] arr = new int[n][m];

    for (int p = 0; p &lt; n; p++) {
      st = new StringTokenizer(br.readLine());
      for (int q = 0; q &lt; m; q++) {
        arr[p][q] = Integer.parseInt(st.nextToken());
      }
    }

    st = new StringTokenizer(br.readLine());
    int k = Integer.parseInt(st.nextToken());

    for (int num = 0; num &lt; k; num++) {
      st = new StringTokenizer(br.readLine());
      int i = Integer.parseInt(st.nextToken()) - 1;
      int j = Integer.parseInt(st.nextToken()) - 1;
      int x = Integer.parseInt(st.nextToken()) - 1;
      int y = Integer.parseInt(st.nextToken()) - 1;
      int sum = 0;

      // i~x, j~y
      for (int p = i; p &lt;= x; p++) {
        for (int q = j; q &lt;= y; q++) {
          sum += arr[p][q];
        }
      }
      System.out.println(sum);
    }
  }
}

</code></pre>
<hr>
<h3 id="여담">여담</h3>
<p>2차원 배열의 합을 구하는 방법이 헷갈렸는데, 해당하는 인덱스만큼만 돌면서 더해주면 되는거였다!</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[[JAVA] 백준 13322: 접두사 배열]]></title>
            <link>https://velog.io/@h2_30r1d/B113322</link>
            <guid>https://velog.io/@h2_30r1d/B113322</guid>
            <pubDate>Fri, 30 Dec 2022 16:11:16 GMT</pubDate>
            <description><![CDATA[<h3 id="문제">문제</h3>
<p>브론즈 1
<a href="https://www.acmicpc.net/problem/13322">https://www.acmicpc.net/problem/13322</a></p>
<h3 id="풀이">풀이</h3>
<pre><code class="language-java">package Baekjoon;
import java.util.Scanner;

public class B1_13322 {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String input = sc.nextLine();

    for (int i = 0; i &lt; input.length(); i++) {
      System.out.println(i);
    }
  }
}

</code></pre>
<hr>
<h3 id="여담">여담</h3>
<p>접두사 배열이라 결국 사전순으로 정렬되어있어서, 문자열 길이만큼 끝나는 인덱스만 찍어주면 되는 문제였다.</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[[JAVA] 백준 2562: 최댓값]]></title>
            <link>https://velog.io/@h2_30r1d/JAVA-%EB%B0%B1%EC%A4%80-2562</link>
            <guid>https://velog.io/@h2_30r1d/JAVA-%EB%B0%B1%EC%A4%80-2562</guid>
            <pubDate>Fri, 30 Dec 2022 16:04:02 GMT</pubDate>
            <description><![CDATA[<h3 id="문제">문제</h3>
<p>브론즈 3
<a href="https://www.acmicpc.net/problem/2562">https://www.acmicpc.net/problem/2562</a></p>
<h3 id="풀이">풀이</h3>
<pre><code class="language-java">package Baekjoon;

import java.util.Scanner;

public class B3_2562 {

  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int[] arr = new int[9];

    for (int i = 0; i &lt; 9; i++) {
      arr[i] = in.nextInt();
    }

    int max = arr[0];
    int idx = 0;

    for (int i = 0; i &lt; 9; i++) {
      if (arr[i] &gt;= max) {
        max = arr[i];
        idx = i + 1;
      }
    }

    System.out.println(max);
    System.out.println(idx);
  }
}
</code></pre>
<hr>
<p>인덱스 처리를 해줘야하기 때문에 if문에서 조건을 arr[i] <strong>&gt;</strong> max로 하면 안된다! 주의!</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[[JAVA] 백준 10807: 개수 세기]]></title>
            <link>https://velog.io/@h2_30r1d/JAVA-%EB%B0%B1%EC%A4%80-10807-%EA%B0%9C%EC%88%98-%EC%84%B8%EA%B8%B0</link>
            <guid>https://velog.io/@h2_30r1d/JAVA-%EB%B0%B1%EC%A4%80-10807-%EA%B0%9C%EC%88%98-%EC%84%B8%EA%B8%B0</guid>
            <pubDate>Fri, 30 Dec 2022 15:57:23 GMT</pubDate>
            <description><![CDATA[<h3 id="문제">문제</h3>
<p><a href="https://www.acmicpc.net/problem/10807">https://www.acmicpc.net/problem/10807</a></p>
<h3 id="풀이">풀이</h3>
<pre><code class="language-java">package Baekjoon;

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

public class B_10807 {
  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    StringTokenizer st = new StringTokenizer(br.readLine());
    int n = Integer.parseInt(st.nextToken());

    int[] arr = new int[n];
    int cnt = 0;

    st = new StringTokenizer(br.readLine());

    for (int i = 0; i &lt; n; i++) {
      arr[i] = Integer.parseInt(st.nextToken());
    }

    st = new StringTokenizer(br.readLine());
    int v = Integer.parseInt(st.nextToken());

    for (int i = 0; i &lt; n; i++) {
      if (arr[i] == v) {
        cnt++;
      }
    }

    System.out.println(cnt);

  }
}
</code></pre>
]]></description>
        </item>
        <item>
            <title><![CDATA[[JAVA] 백준 11659: 구간 합 구하기 4]]></title>
            <link>https://velog.io/@h2_30r1d/JAVA-%EB%B0%B1%EC%A4%80-11659-%EA%B5%AC%EA%B0%84-%ED%95%A9-%EA%B5%AC%ED%95%98%EA%B8%B0-4</link>
            <guid>https://velog.io/@h2_30r1d/JAVA-%EB%B0%B1%EC%A4%80-11659-%EA%B5%AC%EA%B0%84-%ED%95%A9-%EA%B5%AC%ED%95%98%EA%B8%B0-4</guid>
            <pubDate>Thu, 22 Dec 2022 14:49:49 GMT</pubDate>
            <description><![CDATA[<h3 id="문제">문제</h3>
<p>실버 3
<a href="https://www.acmicpc.net/problem/11659">https://www.acmicpc.net/problem/11659</a></p>
<h3 id="풀이">풀이</h3>
<pre><code class="language-java">import java.util.Scanner;

public class S3_11659 {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    int N = in.nextInt();
    int M = in.nextInt();

    int[] input = new int[N];
    int[] sum = new int[N];
    int[] result = new int[M];

    for (int i = 0; i &lt; N; i++) {
      input[i] = in.nextInt();

      if (i == 0) {
        sum[i] = input[i];
      } else {
        sum[i] = sum[i-1] + input[i];
      }
    }

    for (int x = 0; x &lt; M; x++) {
      int i = in.nextInt() - 1;
      int j = in.nextInt() - 1;

      if (i == 0) {
        result[x] = sum[j];
      } else {
        result[x] = sum[j] - sum[i - 1];
      }
    }

    for (int x = 0; x &lt; M ; x++) {
      System.out.println(result[x]);
    }
  }
}
</code></pre>
<hr>
<blockquote>
<p>[Do it! 알고리즘 코딩테스트 자바편]</p>
</blockquote>
<h3 id="과정">과정</h3>
<p><strong>1. 문제 분석 &amp; 풀어보기</strong></p>
<ol>
<li><p>N개의 수를 입력받음과 동시에 합 배열 생성</p>
<pre><code class="language-java">S[i] = S[i-1] + A[i]</code></pre>
</li>
<li><p>구간 i, j가 주어지면 구간 합을 구하는 공식으로 정답 출력</p>
<pre><code class="language-java">S[j] = S[i-1]</code></pre>
</li>
</ol>
<p><strong>2. 슈도코드</strong>
<img src="https://velog.velcdn.com/images/h2_30r1d/post/376569c1-ce63-4a50-8d13-5a91eed3a2f8/image.png" alt=""></p>
<p><strong>3. 코드</strong></p>
<pre><code class="language-java">    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer stringTokenizer = new StringTokenizer(bufferedReader.readLine());

    int suNo = Integer.parseInt(stringTokenizer.nextToken());   // 데이터 개수, 5
    int quizNo = Integer.parseInt(stringTokenizer.nextToken());   // 질의 개수, 3

    long[] S = new long[suNo + 1];   // 1 더해서 6개의 원소를 가진 배열 생성

    stringTokenizer = new StringTokenizer(bufferedReader.readLine());
    for (int i = 1; i &lt;= suNo; i++) {      // 1부터 시작해서
      S[i] = S[i - 1] + Integer.parseInt(stringTokenizer.nextToken());   // 바로 받으면서 합 배열 생성
    }

    for (int q = 0; q &lt; quizNo; q++) {
      stringTokenizer = new StringTokenizer(bufferedReader.readLine());
      int i = Integer.parseInt(stringTokenizer.nextToken());
      int j = Integer.parseInt(stringTokenizer.nextToken());
      System.out.println(S[j] - S[i - 1]);   // 구간 합 구하기
    }
</code></pre>
<hr>
<h3 id="여담">여담</h3>
<p>생각보다 오래 걸렸다...</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[[JAVA] Scanner next, nextLine, nextInt]]></title>
            <link>https://velog.io/@h2_30r1d/JAVA-Scanner-next-nextLine-%EC%B0%A8%EC%9D%B4</link>
            <guid>https://velog.io/@h2_30r1d/JAVA-Scanner-next-nextLine-%EC%B0%A8%EC%9D%B4</guid>
            <pubDate>Tue, 20 Dec 2022 16:25:42 GMT</pubDate>
            <description><![CDATA[<pre><code class="language-java">Scanner in = new Scanner(System.in);
System.out.println(in.next());
// 5 4 3 2 1
// -&gt; 5


System.out.println(in.nextLine());
// 5 4 3 2 1
// -&gt; 5 4 3 2 1
</code></pre>
<p>nextInt는 공백 구분 가능</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[[JAVA] 백준 1546: 평균]]></title>
            <link>https://velog.io/@h2_30r1d/JAVA-%EB%B0%B1%EC%A4%80-1546-%ED%8F%89%EA%B7%A0</link>
            <guid>https://velog.io/@h2_30r1d/JAVA-%EB%B0%B1%EC%A4%80-1546-%ED%8F%89%EA%B7%A0</guid>
            <pubDate>Tue, 22 Nov 2022 15:54:54 GMT</pubDate>
            <description><![CDATA[<h3 id="문제">문제</h3>
<p><a href="https://www.acmicpc.net/problem/1546">https://www.acmicpc.net/problem/1546</a></p>
<hr>
<h3 id="풀이">풀이</h3>
<pre><code class="language-java">import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class B1_1546 {

  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int N = Integer.parseInt(br.readLine());
    int[] grade = new int[N];

    StringTokenizer st = new StringTokenizer(br.readLine());
    // 공백으로 나눠서 배열에 입력
    for (int i = 0; i &lt; N; i++) {
      grade[i] = Integer.parseInt(st.nextToken());
    }

    int M = grade[0];
    double[] grade_d = new double[N];
    double sum = 0;

    // 최댓값 구하기
    for (int g : grade) {
      if (g &gt; M) {
        M = g;
      }
    }
    // 점수 배열 변환
    for (int i = 0; i &lt; N; i++) {
      grade_d[i] = (double) grade[i] / M * 100;
    }
    // 합 구하기
    for (double d : grade_d) {
      sum += d;
    }
    System.out.println(sum / N);
  }
}</code></pre>
<pre><code>    과목 개수 int N 입력 받기
    현재 성적 입력받아서 int[]에 넣기
    M 최댓값
    sum 합
    for(int[] 돌면서)
      더 크면 M에 저장

    for(int[] 돌면서)
      점수 = 점수/M*100

    for(int[] 돌면서)
      합 += 점수

    print 점수/N</code></pre><p>처음에는 이렇게 int 배열 내에서 변환하려고 했는데 정수로 나누고 하다보니 0이 나왔다...생각해보니 당연함...
double 배열을 새로 만들어서 계산해야 올바른 값이 나온다.</p>
<pre><code>    과목 개수 int N 입력 받기
    현재 성적 입력받아서 int[]에 넣기
    double[] 선언
    M 최댓값
    sum 합
    for(int[] 돌면서)
      더 크면 M에 저장

    for(i 돌면서)
      점수 = 점수/M*100 로 double[] 에 넣기

    for(int[] 돌면서)
      합 += 점수

    print 점수/N</code></pre><hr>
<blockquote>
<p>[Do it! 알고리즘 코딩 테스트 자바편]</p>
</blockquote>
<h3 id="과정">과정</h3>
<p><strong>1. 문제 분석 &amp; 풀어보기</strong>
일일이 변환 점수를 구할 필요 없이 한번에 올바른 값을 구할 수 있다.</p>
<pre><code class="language-java">(A + B + C) * 100 / M / 3

-&gt; (A / M * 100 + B / M * 100 + C / M * 100) / 3</code></pre>
<p>1) 점수를 1차원 배열에 저장
2) 배열을 탐색하며 최고 점수와 총합
3) 총합 * 100 / 최고 점수 / 과목 수</p>
<p><strong>2. 슈도코드</strong></p>
<pre><code>변수 N에 과목 수 입력받기
길이가 N인 A[] 선언

for(A[] 길이)
    A[i]에 각 점수 저장

for(A[] 길이)
    max에 최댓값, sum에 총점 저장

sum * 100 / max / N 출력</code></pre><p><strong>3. 코드</strong></p>
<pre><code class="language-java">    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    int A[] = new int[N];
    for (int i = 0; i &lt; N; i++) {
      A[i] = sc.nextInt();
    }
    long sum = 0;
    long max = 0;
    for (int i = 0; i &lt; N; i++) {
      if (A[i] &gt; max) max = A[i];
      sum += A[i];
    }
    System.out.println(sum * 100.0 / max / N);</code></pre>
<hr>
<h3 id="추가">추가</h3>
<p><strong>자동 타입 변환</strong></p>
<pre><code class="language-java">double d1 = 5 * 3.14;
double d2 = 1;</code></pre>
<p>이렇게 하면 int가 double로 자동 타입 변환된다.</p>
<p>기초 타입 범위
double &gt; float &gt; long &gt; int &gt; short, char &gt; byte</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[[JAVA] 백준 11720: 숫자의 합]]></title>
            <link>https://velog.io/@h2_30r1d/JAVA-%EB%B0%B1%EC%A4%80-11720-%EC%88%AB%EC%9E%90%EC%9D%98-%ED%95%A9</link>
            <guid>https://velog.io/@h2_30r1d/JAVA-%EB%B0%B1%EC%A4%80-11720-%EC%88%AB%EC%9E%90%EC%9D%98-%ED%95%A9</guid>
            <pubDate>Tue, 22 Nov 2022 07:49:20 GMT</pubDate>
            <description><![CDATA[<h3 id="문제">문제</h3>
<p> 백준 11720: 브론즈 2
 <a href="https://www.acmicpc.net/problem/11720">https://www.acmicpc.net/problem/11720</a></p>
<h3 id="풀이">풀이</h3>
<pre><code class="language-java">import java.util.Scanner;

public class Main {
  public static void main(String[] args) {

    Scanner in = new Scanner(System.in);
    int a = in.nextInt();
    String b = in.next();
    int sum = 0;

    for (int i = 0; i &lt; b.length(); i++) {
      int num_b = Integer.parseInt(String.valueOf(b.charAt(i)));
      sum += num_b;
    }
    System.out.println(sum);
  }
}
  }
}
</code></pre>
<hr>
<blockquote>
<p>[Do it! 자바 코딩테스트 알고리즘 자바편]</p>
</blockquote>
<h3 id="과정">과정</h3>
<p><strong>1. 문제 분석 &amp; 풀어보기</strong></p>
<p>1) 문자열로 입력값 받고 -&gt; String으로 저장
2) 문자 배열로 변환하고 -&gt; char []로 변환
3) 문자 배열값을 순서대로 읽으면서 -&gt; for문
4) 숫자로 변환해서 -&gt; 아스키코드 연산
5) 더해야한다. -&gt; 결괏값에 누적</p>
<p>🌵 문자열을 숫자로 변경하려면 아스키코드를 이해해야 한다.
같은 의미의 문자와 숫자의 코드 값 차이는 48이다.
ex) 문자 &#39;1&#39;의 아스키코드 값은 49, &#39;0&#39;은 48
문자 &#39;1&#39;을 숫자 1로 변환하려면, <strong>&#39;1&#39;-48, 또는 &#39;1&#39;-&#39;0&#39;</strong>으로 연산</p>
<p><strong>2. 슈도코드</strong></p>
<pre><code>N 입력 받기
길이 N의 숫자를 입력받아 String sNum에 저장
char [] cNum에 sNum 변환해서 저장
int sum = 0;

for (cNum의 길이) {
    cNum[i] int로 바꿔서 += sum
}

sum 출력</code></pre><p><strong>3. 코드</strong></p>
<pre><code class="language-java">    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    String sNum = sc.next();
    char[] cNum = sNum.toCharArray();
    int sum = 0;
    for (int i = 0; i &lt; cNum.length; i++) {
      sum += cNum[i] - &#39;0&#39;;  // 정수형으로 변환
    }
    System.out.println(sum);</code></pre>
<hr>
<h3 id="다른-사람의-풀이">다른 사람의 풀이</h3>
<pre><code class="language-java">        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        char[] numbers = br.readLine().toCharArray();
        int total = 0;

        for (int i = 0; i &lt; N; i++) {
            total += Integer.parseInt(String.valueOf(numbers[i]));
        }
        System.out.println(total);</code></pre>
<p>BufferedReader를 이용해봐야겠다.</p>
<hr>
<h3 id="추가">추가</h3>
<p><a href="https://velog.io/@h2_30r1d/JAVA-%ED%83%80%EC%9E%85-%EA%B0%84-%EB%B3%80%ED%99%98-%EC%A0%95%EB%A6%ACString-int-String-char">자바 형변환(String↔숫자형, String↔char)</a></p>
<p>next(), nextLine()의 차이
<a href="https://devlog-wjdrbs96.tistory.com/80">https://devlog-wjdrbs96.tistory.com/80</a></p>
<hr>
<h3 id="여담">여담</h3>
<p>22.11.21
자바 적응하기 어렵다ㅎ ㅎ</p>
]]></description>
        </item>
        <item>
            <title><![CDATA[[JAVA] 타입 간 변환 정리(String ↔ 숫자형, String ↔ char)]]></title>
            <link>https://velog.io/@h2_30r1d/JAVA-%ED%83%80%EC%9E%85-%EA%B0%84-%EB%B3%80%ED%99%98-%EC%A0%95%EB%A6%ACString-int-String-char</link>
            <guid>https://velog.io/@h2_30r1d/JAVA-%ED%83%80%EC%9E%85-%EA%B0%84-%EB%B3%80%ED%99%98-%EC%A0%95%EB%A6%ACString-int-String-char</guid>
            <pubDate>Tue, 22 Nov 2022 07:37:24 GMT</pubDate>
            <description><![CDATA[<h2 id="1-string-→-숫자형int-double-float-long-short">1. String → 숫자형(int, double, float, long, short)</h2>
<pre><code class="language-java">String str = &quot;1234&quot;;

int i1 = Integer.parseInt(str);  // int 리턴
int i2 = Integer.valueOf(str);  // Integer Object 리턴
// Integer.valueOf(str).intValue(); -&gt; Integer Object를 int로 변환할 수 있음</code></pre>
<p>double, float, long, short도 마찬가지로 하면 된다.
Double.parseDouble(str), Double.valueOf(str) 이런 식!</p>
<blockquote>
<p><strong>int와 Integer의 차이</strong>
<a href="https://velog.io/@h2_30r1d/JAVA-wrapper-class-%EC%A0%95%EB%A6%ACint%EC%99%80-Integer%EC%9D%98-%EC%B0%A8%EC%9D%B4">primitive 타입과 wrapper class</a></p>
</blockquote>
<hr>
<h2 id="2-숫자형int-double-float-long-short-→-string">2. 숫자형(int, double, float, long, short) → String</h2>
<pre><code class="language-java">int i = 1234;

String i1 = String.valueOf(i);
String i2 = Integer.toString(i);
</code></pre>
<p>double, float, long, short도 마찬가지로 하면 된다.
double d가 있다면, String.valueof(d), Double.toString(d) 이런 식!</p>
<hr>
<h2 id="3-string-→-char">3. String → char</h2>
<h3 id="char-배열로-변환">char 배열로 변환</h3>
<pre><code class="language-java">String str = &quot;hello&quot;;

char[] charArray = str.toCharArray();</code></pre>
<h3 id="stream-방식">stream 방식</h3>
<pre><code class="language-java">String str = &quot;hello&quot;;

str.chars().mapToObj(ch -&gt; (char)ch).forEach(System.out::println);</code></pre>
<h3 id="charat으로-특정-인덱스에-있는-문자-가져오기">charAt()으로 특정 인덱스에 있는 문자 가져오기</h3>
<pre><code class="language-java">String str = &quot;hello&quot;;

char c = str.charAt(인덱스);</code></pre>
<hr>
<h2 id="4-char-→-string">4. char → String</h2>
<h3 id="valueofchar">valueOf(char)</h3>
<pre><code class="language-java">char c = &#39;a&#39;;

String str = String.valueOf(c);

// 같은 방식으로 char[] 배열도 String으로 변환 가능</code></pre>
<h3 id="tostring">toString()</h3>
<pre><code class="language-java">char c = &#39;a&#39;;

String str = Character.toString(c);

// char[] 배열은 변환 불가능</code></pre>
<hr>
<h2 id="참고">참고</h2>
<p><a href="https://codechacha.com/ko/java-convert-string-to-chararray/">https://codechacha.com/ko/java-convert-string-to-chararray/</a></p>
<p>char -&gt; String
<a href="https://codechacha.com/ko/java-convert-chararray-to-string/">https://codechacha.com/ko/java-convert-chararray-to-string/</a></p>
<p><a href="https://java119.tistory.com/106">https://java119.tistory.com/106</a></p>
]]></description>
        </item>
    </channel>
</rss>