[BOJ]11723 ์งํฉ (java,kotlin)
โ๋ฌธ์ โ
๐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()
}
๋๊ธ๋จ๊ธฐ๊ธฐ