백준 11723번
https://www.acmicpc.net/problem/11723
비어있는 공집합 S가 주어졌을 때, 아래 연산을 수행하는 프로그램을 작성하시오.
Set 자료구조를 통해서 명령어에 대해서 구현만 간단히 하면 된다.
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
HashSet<Integer> set = new HashSet();
int M = Integer.parseInt(br.readLine());
while(M-->0) {
StringTokenizer st = new StringTokenizer(br.readLine());
String str = st.nextToken();
int x = 0;
if( !str.equals("all") && !str.equals("empty") ) x = Integer.parseInt(st.nextToken());
if(str.equals("add")) set.add(x);
else if(str.equals("remove")) set.remove(x);
else if(str.equals("check")) {
if(set.contains(x)) sb.append(1).append('\n');
else sb.append(0).append('\n');
}
else if(str.equals("toggle")) {
if(set.contains(x)) set.remove(x);
else set.add(x);
}
else if(str.equals("all")) {
for(int i=1; i<=20; i++) set.add(i);
}
else if(str.equals("empty")) set.clear();
}
bw.write(sb.toString()); bw.flush(); bw.close();
} // End of main
} // End of Main class
import java.util.*
import java.io.*
import kotlin.collections.HashSet
fun main() {
val br = BufferedReader(InputStreamReader(System.`in`))
val bw = BufferedWriter(OutputStreamWriter(System.`out`))
val sb = StringBuilder()
val set = HashSet<Int>()
var M = br.readLine().toInt()
while(M-->0) {
val st = StringTokenizer(br.readLine())
val str = st.nextToken()
var x = 0
if( !str.equals("all") && !str.equals("empty")) x = st.nextToken().toInt()
if(str.equals("add")) set.add(x);
else if(str.equals("remove")) set.remove(x);
else if(str.equals("check")) {
if(set.contains(x)) sb.append(1).append('\n');
else sb.append(0).append('\n');
}
else if(str.equals("toggle")) {
if(set.contains(x)) set.remove(x);
else set.add(x);
}
else if(str.equals("all")) {
for(i in 1..20) set.add(i);
}
else if(str.equals("empty")) set.clear();
}
bw.write(sb.toString()); bw.flush(); bw.close()
} // End of main