Algorithm

๋ฐฑ์ค€ 10818 | ์ตœ์†Œ, ์ตœ๋Œ“๊ฐ’ ๊ตฌํ•˜๊ธฐ

osean 2020. 10. 16. 19:03

์ตœ์†Œ, ์ตœ๋Œ“๊ฐ’ ๊ตฌํ•˜๊ธฐ

๋ฉด์ ‘ ์ค€๋น„๋ฅผ ํ•˜๋ฉด์„œ ์„ ํƒ ์ •๋ ฌ๊ณผ ๋ฒ„๋ธ” ์ •๋ ฌ์ด ๋ฌธ์ œ๋กœ ๋‚˜์˜จ๋‹ค๊ณ  ๋“ค์–ด์„œ ์„ ํƒ ์ •๋ ฌ์— ๋Œ€ํ•ด์„œ ๋จผ์ € ๊ณต๋ถ€๋ฅผ ํ–ˆ๋‹ค. ์˜ˆ์ „์— ํ’€์—ˆ๋˜ ๋ฌธ์ œ์ง€๋งŒ ๋‹ค์‹œ ๋“ค์—ฌ๋‹ค๋ณด์ง€ ์•Š์•„์„œ ๋‹ค ๊นŒ๋จน์—ˆ๋‹ค. ์–ด์จŒ๋“ , ๋‚˜๋Š” ์ด๋ฒˆ ๋ฌธ์ œ๋ฅผ ์„ ํƒ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ™œ์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค. ๊ทผ๋ฐ ๊ฒฐ๊ณผ๋Š” ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ์˜€๋‹ค.

  • ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ ์ฝ”๋“œ

      import java.io.*;
    
      public class Main {
    
          public static void main(String[] args) throws Exception {
    
              BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
              int n = Integer.parseInt(br.readLine());
    
              int[] list = new int[n];
    
              for (int i = 0; i < n; i++) {
                  list[i] = Integer.parseInt(br.readLine());
              }
    
              int min = list[0];
              int max = list[0];
    
              for (int i = 0; i < n; i++) {
                  for (int k = i + 1; k < n; k++) {
                      boolean minCheck = min > list[k];
                      boolean maxCheck = max < list[k];
                      if (minCheck)
                          min = list[k];
                      if (maxCheck)
                          max = list[k];
    
                  }
    
              }
              System.out.println(min + " " + max);
          }
    
      }

๋ถ„๋ช… IDE์—์„œ๋Š” ์ฃผ์–ด์ง„ ์˜ˆ์ œ์˜ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์—ˆ์„ ๋•Œ ๋™์ผํ•œ ๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋˜๋Š”๋ฐ, ์™œ ์ œ์ถœํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ด์„œ ์ฑ„์ ์กฐ์ฐจ ์•ˆ๋˜๋Š”์ง€ ์•Œ ์ˆ˜๊ฐ€ ์—†์—ˆ๋‹ค.
๋˜‘๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ํ˜•์‹๋งŒ ๋ฐ”๊ฟ”์„œ 10๋ฒˆ ์ •๋„ ๋‹ค์‹œ ๋„ฃ์–ด ๋ด๋„ ๊ฒฐ๊ณผ๋Š” ๋˜‘๊ฐ™์•˜๋‹ค.

BufferReader

์ด๋ฒˆ ์ฝ”๋“œ๋Š” Scannerํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  BufferReaderํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.
๊ณฐ๊ณฐ์ด ์ƒ๊ฐํ–ˆ์„ ๋•Œ, ๋‚ด๊ฐ€ ์œ ์ถ”ํ•  ์ˆ˜ ์žˆ๋Š” ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ์˜ ์ด์œ ๋Š” ๋ฐ”๋กœ ์ด๊ฑฐ๋‹ค.

์ž…๋ ฅ ์˜ˆ์ œ๋ฅผ ๋ถ™์—ฌ ๋„ฃ์—ˆ์„ ๋•Œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์ด์œ ๋Š” ์ฑ„์  ์‹œ, ์‚ฝ์ž…ํ•˜๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ๋‚ด ์ฝ”๋“œ์— ๋งž์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค!

์ฝ”๋“œ์—์„œ BufferReader์˜ ๋ฉ”์†Œ๋“œ ์ค‘ readLine() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค ์ž…๋ ฅ ์˜ˆ์ œ๋ฅผ ๋ฐ›์•˜๋Š”๋ฐ, ํ•ด๋‹น ๋ฉ”์†Œ๋“œ๋Š” ํ•œ ์ค„๋กœ ์ž…๋ ฅํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ฐ›๊ณ , ์ด๋ฅผ Integer๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ๊ณผ์ •์—์„œ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ ๊ฐ™๋‹ค. ์ฆ‰, ๋„์–ด์“ฐ๊ธฐ๋งŒํผ ๋ฌธ์ž์—ด์„ ์ž˜๋ผ๋‚ด๊ณ , ์ด๋ฅผ ๊ธฐ๋ณธ ์ž๋ฃŒํ˜•์œผ๋กœ ๋ฐ”๊ฟ”์•ผ ํ–ˆ๋‹ค.

Scanner๋ฅผ ์‚ฌ์šฉํ–ˆ๋”๋ผ๋ฉด, next() ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ๋„์–ด์“ฐ๊ธฐ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋‚˜๋ˆ„๋ฉด ๋๋Š”๋ฐ, BufferReader๋Š” ๊ทธ๊ฒŒ ์•ˆ๋˜๋Š” ๊ฒƒ ๊ฐ™์•˜๋‹ค.
(์•„๋งˆ ๋” ํ˜„๋ช…ํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ์„์ง€๋„..)
ํ•˜์ง€๋งŒ ๋‚˜๋Š” Scanner๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์ง€ ์•Š์•˜๋Š”๋ฐ, ๋ฌด์—‡๋ณด๋‹ค BufferReader๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ ๋กœ์ง์„ ํ•ด๊ฒฐํ•˜๋Š” ๋Ÿฐํƒ€์ž„ ์‹œ๊ฐ„์ด ๊ต‰์žฅํžˆ ์ค„์—ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.(๋‹ค๋ฅธ ์‚ฌ๋žŒ๋“ค ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ˆ ๊ทธ๋Ÿฌํ–ˆ๋‹ค.)
๊ทธ๋Ÿฌํ•œ ์ด์œ ๋กœ BufferReader๋ฅผ ์ž˜ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ๋ญ๊ฐ€ ์žˆ์„๊นŒ ๊ฒ€์ƒ‰ํ–ˆ๋‹ค.

StringTokenizer

๊ตฌ๊ธ€ ๊ฒ€์ƒ‰์ด๋‚˜ ๋งž์€ ์‚ฌ๋žŒ๋“ค ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•ด๋ณด๋‹ˆ StringTokenizer๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค. ์ด ํด๋ž˜์Šค๋Š” ๋ฌธ์ž์—ด์„ ํŠน์ • ๊ตฌ๋ถ„์ž๋ฅผ ์ง€์ •ํ•˜๊ณ , ์ด๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ์ž˜๋ผ๋‚ด๊ณ , ์ƒ์„ฑ์ž๋ฅผ ์„ ์–ธํ•œ ๋ณ€์ˆ˜์— ์ €์žฅํ•œ๋‹ค. ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด

  1. ์ตœ์†Œ, ์ตœ๋Œ“๊ฐ’์„ ๊ฒ€์‚ฌํ•  ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค.
  2. ์ž…๋ ฅ ๋ฐ›์€ ์ •์ˆ˜๋ฅผ ์ผ์ผ์ด ๊ฒ€์‚ฌํ•œ๋‹ค.

์ด ๊ณผ์ •์ด ์ค„์–ด๋“ค์–ด ์ž…๋ ฅ ๋ฐ›๋Š” ๋™์‹œ์— ์ตœ์†Œ, ์ตœ๋Œ“๊ฐ’ ๊ฒ€์‚ฌ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.
๋˜ํ•œ, ํ•ด๋‹น ํด๋ž˜์Šค๋Š” Set ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‘์šฉํ•ด์„œ ์ž‘๋™ํ•˜๋Š” ๊ฒƒ ๊ฐ™์•˜๋‹ค.
์™œ๋ƒํ•˜๋ฉด ์ฒ˜์Œ DAO, DTO์— ๋Œ€ํ•œ ๊ฐœ๋…์„ ๋ฐฐ์šธ ๋•Œ ResultSet์ด๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ์ด์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์•„์˜ค๊ณ , next() ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•ด ๋ฐ›์•„ ์˜ฌ Rownum ๊ฐœ์ˆ˜๋งŒํผ while๋ฌธ์„ ๋Œ๋ฆฌ๋Š” ๊ฒƒ์ด ๋– ์˜ฌ๋ž๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

  • BufferReader + StringTokenizer๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ

      import java.io.BufferedReader;
      import java.io.InputStreamReader;
      import java.util.StringTokenizer;
    
      public class Main {
    
          public static void main(String[] args) throws Exception {
    
              BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
              Integer.parseInt(br.readLine()); // ๋ฐฐ์—ด์˜ ๊ธธ์ด
    
              StringTokenizer st = new StringTokenizer(br.readLine(), " ");
    
              int min = 1000001;
              int max = -1000001;
    
              while (st.hasMoreTokens()) {
    
                  int value = Integer.parseInt(st.nextToken());
    
                  if (max < value)
                      max = value;
    
                  if (min > value)
                      min = value;
              }
              System.out.println(min + " " + max);
          }
    
      }

์œ„์˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ์•Œ ์ˆ˜ ์žˆ๋“ฏ, ์ž…๋ ฅ ๋ฐ›์€ ๋ฌธ์ž์—ด์„ ๋ฐ˜๋ณต๋ฌธ ์•ˆ์—์„œ int๋กœ ์žฌ์ •์˜ํ–ˆ๋‹ค. ๋˜ํ•œ, max์™€ min๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด, ๋…ผ๋ฆฌ๊ฐ’์ด true์ธ ๊ฒฝ์šฐ์— ๊ฐ ๋ณ€์ˆ˜์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ํ˜„์žฌ์˜ value๊ฐ’์ด ๋˜๋Š” ๊ตฌ์กฐ๋กœ ๋งŒ๋“ค์—ˆ๋‹ค.
(์‚ฌ์‹ค StringTokenizer๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•˜๋‹ค ๋ณด๋‹ˆ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ์ง  ์ฝ”๋“œ๋ฅผ ๋ณผ ์ˆ˜ ๋ฐ–์— ์—†์—ˆ๋‹ค..์“ฐ๋ ‰์„ฑํ—Œ..๐Ÿ—‘)

๋งˆ๋ฌด๋ฆฌ

์•„์ฃผ ์˜ˆ์ „์— ์ด ๋ฌธ์ œ๋ฅผ ํ‘ผ ์ ์ด ์žˆ๋Š”๋ฐ, ๊ทธ ๋•Œ๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋ญ”์ง€ ๋ฌธ์ œ๋Š” ์–ด๋–ป๊ฒŒ ํ’€์–ด์•ผ ํ• ์ง€๋„ ๋ชฐ๋ผ์„œ ์ดํ•ดํ•˜์ง€๋„ ๋ชปํ•œ ๋‚จ์˜ ์ฝ”๋“œ ๊ฐ–๋‹ค ๋ถ™์—ฌ ๋„ฃ์–ด์„œ ๋งž์€ ๊ธฐ๋ก์ด ์žˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๊ทธ๋Ÿฌ๊ณ  ์‹ถ์ง€ ์•Š์•˜์ง€๋งŒ ์˜๋„์น˜ ์•Š๊ฒŒ(?) ์ด๋Ÿฐ ์ƒํ™ฉ์ด ๋œ ์  ์กฐ๊ธˆ ์†์ƒํ•˜๋‹ค. ๊ทธ๋ž˜๋„ ๊ทธ ๋•Œ์™€๋Š” ๋‹ฌ๋ฆฌ ์ฝ”๋“œ ์ž์ฒด๊ฐ€ ์ดํ•ด๊ฐ€ ๋˜๊ณ , ๋”ฐ๋กœ ๊ฒ€์ƒ‰ํ•˜์ง€ ์•Š์•„๋„ ๋‚ด๊ฐ€ ๋ชจ๋ฅด๋Š” ํด๋ž˜์Šค๋ฅผ ์™œ ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ–ˆ๋Š”์ง€ ์ฝํžˆ๊ณ  ์žˆ๋‹ค.
์–ด์จŒ๋“ , ์˜ˆ์ „์— ์ œ์ถœํ–ˆ๋˜ ์ฝ”๋“œ์™€ ์ง€๊ธˆ ์ œ์ถœํ•œ ์ฝ”๋“œ์™€ ๋น„๊ต๋ฅผ ํ•˜๋ฉด

  • ๊ณผ๊ฑฐ
    1. ListArray ํด๋ž˜์Šค ์•ˆ์— ์ตœ์†Œ,์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•ด ์ฃผ๋Š” ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ๊ณ , ํ•ด๋‹น ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉ.
    2. Scanner ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•จ.
    3. ๊ทธ๋ž˜์„œ ๋Ÿฐํƒ€์ž„์ด ์˜ค๋ž˜ ๊ฑธ๋ฆผ.
  • ํ˜„์žฌ
    • List์˜ ํ•˜์œ„ ํด๋ž˜์Šค๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ.
    • Scanner๋Œ€์‹  BufferReader + StringTokenizer ์‚ฌ์šฉ.
    • ๋Ÿฐํƒ€์ž„์ด ํ™•์‹คํžˆ ์ค„์–ด๋“ฌ.

์ด๋Ÿฐ ๊ฒฐ๊ณผ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.
์ตœ๋Œ€ํ•œ ์„ ํƒ ์ •๋ ฌ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ™œ์šฉํ•ด์„œ ํ’€์—ˆ๋‹ค๋ฉด ์ข‹์•˜์„ ๊ฒƒ ๊ฐ™์€๋ฐ, ๋งํ•  Scanner๋•Œ๋ฌธ์— ๋‹ค๋ฅธ ์‚ฌ๋žŒ ์ฝ”๋“œ๋Š” ์กฐ๊ธˆ ํ›”์ณ๋ดค๋‹ค. ์•ž์œผ๋กœ๋Š” ๊ทธ๋Ÿฌ์ง€ ๋ง์•„์•ผ์ง€..