21.04.14 [HackerRank] Java Stdin, Stdout, If-Else

์„œํƒœ์šฑยท2022๋…„ 4์›” 14์ผ
0

Algorithm

๋ชฉ๋ก ๋ณด๊ธฐ
2/45
post-thumbnail

๐ŸŒฑ Java Stdin and Stdout I ๋ฐฐ๊ฒฝ์ง€์‹

stdin(standard input) stdout(standard out)์€ ํ‘œ์ค€์ž…๋ ฅ์ด๋ผ๋Š” ๋œป์ด๋‹ค.
stdin์—์„œ ์ผ๋ฐ˜์ ์œผ๋กœ ์ž…๋ ฅ์„ ์ฝ๋Š” ๋ฐฉ๋ฒ•์€ scanner ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ์„ System.in์œผ๋กœ ์ง€์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

  • Scanner class์˜ ํŠน์ง•
  1. ๊ธฐ๋ณธ์ ์ธ ๋ฐ์ดํ„ฐ ํƒ€์ž…๋“ค์„ ์ด ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด ์ž…๋ ฅ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.
    String(๋ฌธ์ž์—ด)์˜ ๊ฒฝ์šฐ next() or nextLine()
    int(์ •์ˆ˜)์˜ ๊ฒฝ์šฐ nextInt()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  2. Scanner ์‚ฌ์šฉ ์‹œ util ํŒจํ‚ค์ง€๋ฅผ ํ†ตํ•ด ํด๋ž˜์Šค ํ˜ธ์ถœ์„ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
import java.util.*;

3.๊ณต๋ฐฑ ๋˜๋Š” ๊ฐœํ–‰์„ ๊ธฐ์ค€์œผ๋กœ ์ฝ๋Š”๋‹ค.
์ž…๋ ฅ ๋ฉ”์„œ๋“œ๋Š” ๋Œ€๋ถ€๋ถ„ ๊ณต๋ฐฑ๊ณผ ๊ฐœํ–‰(' ', '\t', '\r', '\n' ๋“ฑ๋“ฑ..) ๊ธฐ์ค€์œผ๋กœ ์ฝ๋Š”๋‹ค. ์ด๋กœ์จ ์‚ฌ์šฉ์ž ํŽธ์˜์— ๋”ฐ๋ผ ์ž…๋ ฅ์„ ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.

โœ๏ธ Java Stdin and Stdout I ํ’€์ด

import java.util.*; //์Šค์บ๋„ˆ ์‚ฌ์šฉ์„ ์œ„ํ•ด ํ˜ธ์ถœํ•ด ์ฃผ๋Š” ๋ถ€๋ถ„์ด๋‹ค. util ๋‹ค์Œ์— scanner๋ผ๊ณ  ์จ์ค˜๋„ ๋˜์ง€๋งŒ, ์ „์ฒด๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” *๊ฐ€ HackerRank์˜ default์ธ ๋“ฏ ํ•˜๋‹ค.

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in); // ์ž…๋ ฅ์„ ์œ„ํ•ด ์Šค์บ๋„ˆ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด ์ค€๋‹ค. ํด๋ž˜์Šค์ด๋ฆ„ ๊ฐ์ฒด์ด๋ฆ„ = new ํด๋ž˜์Šค์ด๋ฆ„();์˜ ํ˜•ํƒœ.
        // ์ด๋•Œ, ()์•ˆ์—๋Š” ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ž…๋ ฅ์„ ๋ฐ›๊ธฐ ์œ„ํ•œ ์ž…๋ ฅ ์ŠคํŠธ๋ฆผ์ธ System.in์ด ๋“ค์–ด๊ฐ„๋‹ค. 
        int a = scan.nextInt(); // ๋ฌธ์ œ์—์„œ ๊ฐ๊ฐ ์ •์ˆ˜ํ˜• a,b,c๋ฅผ ์ž…๋ ฅ๋ฐ›์œผ๋ฏ€๋กœ
        int b = scan.nextInt(); //intํ˜• ์ž…๋ ฅ ๋ฐ ๋ฆฌํ„ด์„ ํ•ด์ค€๋‹ค.
        int c = scan.nextInt();

        System.out.println(a); // ์ž…๋ ฅ์ด ๋๋‚˜๋ฉด Scanner ๊ฐœ์ฒด๋ฅผ ๋‹ซ๊ณ 
        System.out.println(b); // System.out.println(String)์„ ์‚ฌ์šฉํ•ด
        System.out.println(c); // stdout์— ์ถœ๋ ฅํ•œ๋‹ค.
    }
}

๐ŸŒฑ Java If-Else ๋ฐฐ๊ฒฝ์ง€์‹


๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ If-Else๋ฌธ์˜ ๋…ผ๋ฆฌ์  ํ๋ฆ„๋„์ด๋‹ค.

  • scanner.skip
    scanner์— ๋‚จ์•„์žˆ๋Š” ๋ถˆํ•„์š”ํ•œ ๊ฒƒ๋“ค์„ ์ œ๊ฑฐํ•˜๋Š” ์ฝ”๋“œ๋ผ๊ณ  ํ•œ๋‹ค.
    ์—ฌ๋Ÿฌ ์ค„์˜ ์ž…๋ ฅ ๊ฐ’์„ ๋ฐ›์•„์•ผ ํ•˜๋Š” ์ƒํ™ฉ์—์„œ ์ž…๋ ฅ๊ฐ’์ด ํฌํ•จ๋˜์–ด ์žˆ์ง€ ์•Š์€ ์—”ํ„ฐ๋„
    ํ•œ์ค„๋กœ ์ธ์‹ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๊ทธ๋Ÿฐ ์ƒํ™ฉ์„ ํ”ผํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ.

    /r/n Windows line ending
    /n UNIX line ending
    /r Macintosh (pre-OSX) line ending
    \u2028 LINE SEPARATOR
    \u2029 PARAGRAPH SEPARATOR
    \u0085 NEXT LINE(NEL)

โœ๏ธ Java If-Else ํ’€์ด

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {

    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int N = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
        if(N%2 == 0){ //n์ด ์ง์ˆ˜์ธ ๊ฒฝ์šฐ
            if(N >= 2 && N <=5){ // 2๋ณด๋‹ค ํฌ๊ณ  5๋ณด๋‹ค ์ž‘์€ ๊ฒฝ์šฐ 
                System.out.println("Not Weird");
            }
            else if(N >=6 && N <=20){ // 6๋ณด๋‹ค ํฌ๊ณ  20๋ณด๋‹ค ์ž‘์€ ๊ฒฝ์šฐ 
                System.out.println("Weird");
            }
            else if(N >=20){ // 20๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ
                System.out.println("Not Weird");
            }
        }
         else { //n์ด ํ™€์ˆ˜์ธ ๊ฒฝ์šฐ
                System.out.println("Weird");
            }
        scanner.close();
    }
}

๐ŸŒฑ Java Stdin and Stdout II ๋ฐฐ๊ฒฝ์ง€์‹

๋ฌธ์ œ๋Š” ์ •์ˆ˜(int), ์‹ค์ˆ˜(double), ๋ฌธ์ž์—ด(String)์„ ์ฝ์€ ๋‹ค์Œ
์ถœ๋ ฅ ํ˜•์‹์— ๋”ฐ๋ผ ๊ฐ’์„ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.

  • ํ•ด๋‹น ๋ฌธ์ œ ์ฃผ์˜์ !
    int์™€ double ์ˆœ์„œ๋กœ ์ž…๋ ฅ๋ฐ›์€ ์ดํ›„ String ์ž…๋ ฅ์„ ์œ„ํ•ด ์—”ํ„ฐ๋ฅผ ์น˜๋ฉด
    ๋ฐ”๋กœ output์ด ๋‚˜์˜ค๊ฒŒ ๋œ๋‹ค.
    nextInt();์ดํ›„ newline(\n)์ด ๋ฒ„ํผ์— ๊ฐœํ–‰๋ฌธ์ž๋กœ ๋‚จ์•„
    nextLine();์ด ์ธ์‹ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋ผ๊ณ  ํ•œ๋‹ค.
    ๋”ฐ๋ผ์„œ ๋‚จ์•„ ์žˆ๋Š” ๊ฐœํ–‰๋ฌธ์ž๋ฅผ nextLine()์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฒ„ํผ๋ฅผ ๋น„์›Œ ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

โœ๏ธ Java Stdin and Stdout II ํ’€์ด

import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int i = scan.nextInt();
        
        double d = scan.nextDouble(); 
        scan.nextLine(); // ์ด ๋ถ€๋ถ„์„ ์ถ”๊ฐ€ํ•˜์—ฌ ๋ฒ„ํผ์— ๋‚จ์€ ๊ฐœํ–‰๋ฌธ์ž๋ฅผ ์ฒ˜๋ฆฌํ•ด ์ค€๋‹ค.
        String s = scan.nextLine();
        
        System.out.println("String: " + s);
        System.out.println("Double: " + d);
        System.out.println("Int: " + i);
    }
}

๐Ÿ‘‰ ์ฐธ๊ณ 

0๊ฐœ์˜ ๋Œ“๊ธ€