코딩테스트/백준
[백준] 11723번 집합 (C++)
Deff_a
2024. 8. 30. 16:34
문제

https://www.acmicpc.net/problem/11723
풀이
이 문제는 명령어를 입력받고 명령에 맞는 연산을 수행하는 프로그램을 만드는 문제이다.
map을 이용하여 명령에 맞는 연산을 하는 방식으로 풀이했다.
#include<iostream>
#include<map>
#include<string>
// https://www.acmicpc.net/problem/11723
using namespace std;
map <int, bool> s;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, sel;
string str;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> str;
if (str == "all" || str == "empty") {
if (str == "all") {
for (int j = 1; j <= 20; j++) {
s[j] = true;
}
}
else if (str == "empty") {
for (int j = 1; j <= 20; j++) {
s[j] = false;
}
}
}
else {
cin >> sel;
if (str == "add") {
s[sel] = true;
}
else if (str == "remove") {
s[sel] = false;
}
else if (str == "check") {
int check = s[sel] == true ? 1 : 0;
cout << check << "\n";
}
else if (str == "toggle") {
s[sel] = s[sel] == true ? false : true;
}
}
}
return 0;
}