시간복잡도에 유의하여 풀이해야 한다. 인덱스 배열을 만들어 해결했다.
#include <iostream>
using namespace std;
int M;
int S[22];
int main() {
cin.tie(NULL);
ios::sync_with_stdio(false);
cin >> M;
string inputoper;
int input;
for (int i = 0; i < M; i++)
{
cin >> inputoper;
if (inputoper == "add")
{
cin >> input;
if (!S[input]) S[input] = 1;
}
else if (inputoper == "remove")
{
cin >> input;
if (S[input])
{
S[input] = 0;
}
}
else if (inputoper == "check")
{
cin >> input;
cout << S[input];
cout << '\n';
}
else if (inputoper == "toggle")
{
cin >> input;
if (S[input])
{
S[input] = 0;
}
else
{
S[input] = 1;
}
}
else if (inputoper == "all")
{
for (int i = 1; i < 21; i++)
{
S[i] = 1;
}
}
else if (inputoper == "empty")
{
for (int i = 1; i < 21; i++)
{
S[i] = 0;
}
}
}
return 0;
}
'프로그래밍 공부 > 백준 (C++)' 카테고리의 다른 글
[C++ / 백준 14391번] 종이 조각 (0) | 2025.02.20 |
---|---|
[C++ / 백준 1182번] 부분 수열의 합 (0) | 2025.02.19 |
[C++ / 백준 1248번] Guess(맞춰봐) (0) | 2025.02.05 |
[C++ / 백준 2529번] 부등호 (0) | 2025.01.13 |
[C++ / 백준 14889번] (재귀 / 비트마스크) 스타트와 링크 (0) | 2025.01.07 |