โ”๋ฌธ์ œโ”

11723. ์ง‘ํ•ฉ

๐Ÿ™ŒSolution๐Ÿ™Œ

๋ญ”๊ฐ€ ๋ฌธ์ œ๋ฅผ ์ฝ์œผ๋ฉด์„œ set์„ ์‚ฌ์šฉํ•˜๋ฉด ๋”ฑ ์ข‹๊ฒ ๋‹ค ์ƒ๊ฐ์„ ํ–ˆ๋Š”๋ฐ Set์˜ ์˜๋ฏธ๊ฐ€ ์ง‘ํ•ฉ์ด๋ผ๋Š” ์˜๋ฏธ์˜€๋‹ค.๐Ÿ˜…

import java.io.*;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class Main {

    static private Set<Integer> set= new HashSet <Integer>();

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

        int m=Integer.parseInt(br.readLine().trim());
        for(int i=0;i<m;i++){
            String[] input=br.readLine().trim().split(" ");
            switch(input[0]){
                case "add" : add(Integer.parseInt(input[1]));
                    break;
                case "remove" : remove(Integer.parseInt(input[1]));
                    break;
                case "check" : bw.write(check(Integer.parseInt(input[1]))+"\n");
                    break;
                case "toggle" : toggle(Integer.parseInt(input[1]));
                    break;
                case "all" : all();
                    break;
                case "empty" : empty();
            }
        }
        bw.flush();
    }

    static void add(int x){ set.add(x); }
    static void remove(int x){ set.remove(x); }
    static int check(int x){return (set.contains(x))?1:0;}
    static void toggle(int x){
        if(!set.contains(x))set.add(x);
        else set.remove(x);
    }
    static void all(){
        IntStream st=IntStream.rangeClosed(1,20);
        set=new HashSet<Integer>();
        set.addAll(st.boxed().collect(Collectors.toList()));

    }
    static void empty(){
        set=new HashSet<Integer>();
    }
}

๐Ÿš€Advanced๐Ÿš€

  • IO์‹œ๊ฐ„ ์ค„์ด๊ธฐ ์œ„ํ•ด String,String์„ ๊ฒ€์‚ฌํ•˜์ง€ ์•Š๊ณ  ์•ž
  • ์ž…๋ ฅ ํŠน์„ฑ ์ƒ(1~20) Set์„ ์•ˆ ์“ฐ๊ณ  ๋ฐฐ์—ด๋กœ ๊ตฌํ˜„๋„ ๊ฐ€๋Šฅ
  • set ํƒ€์ž…์„ ๊ตณ์ด Integer๋กœ ํ•˜์ง€ ์•Š๊ณ  String์œผ๋กœ ํ•˜๋Š”๊ฒƒ๋„ ๊ฐ™์€ ๊ฒฐ๊ณผ์ผ ๋“ฏ

๐Ÿ” Language Change(kotlin)๐Ÿ” 

import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.InputStreamReader
import java.io.OutputStreamWriter

fun main(){

    val br= BufferedReader(InputStreamReader(System.`in`))
    val bw= BufferedWriter(OutputStreamWriter(System.out))

    val m=br.readLine().toInt()
    var set=mutableSetOf<String>()
    for(i in 0 until m){
        val input=br.readLine().split(" ")
        when(input[0]){
            "add"->set.add(input[1])
            "remove"-> set.remove(input[1])
            "check"->bw.write(if(set.contains(input[1])) "1\n" else "0\n")
            "toggle"->if(set.contains(input[1]))set.remove(input[1])else set.add(input[1])
            "all"-> set= List(20){"${it+1}"}.toMutableSet()
            "empty"->set= mutableSetOf()
        }
    }
    bw.flush()
}

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ