[백준] 11723 집합

0

백준

목록 보기
198/271
post-thumbnail

[백준] 11723 집합

#include <algorithm>
#include <string>
#include <iostream>
using namespace std;

typedef long long ll;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	ll set = 0LL;

	int m;
	cin >> m;
	while (m--) {
		string input;
		cin >> input;

		if (input == "add") {
			int x;
			cin >> x;
			x--;
			set |= (1 << x);
		}
		else if(input == "remove") {
			int x;
			cin >> x;
			x--;
			set &= ~(1 << x);
		}
		else if (input == "check") {
			int x;
			cin >> x;
			x--;
			if (set & (1 << x)) cout << "1\n";
			else cout << "0\n";
		}
		else if (input == "toggle") {
			int x;
			cin >> x;
			x--;
			set ^= (1 << x);
		}
		else if (input == "all") {
			set = (1 << 20) - 1;
		}
		else if (input == "empty") {
			set = 0LL;
		}
	}

	return 0;
}
  • 벡터 이용 풀이
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);

	vector<bool> vec(21, false);

	int M;
	cin >> M;
	while (M--) {
		string op;
		cin >> op;
		if (op == "add") {
			int x;
			cin >> x;
			vec[x] = true;
		}
		else if (op == "remove") {
			int x;
			cin >> x;
			vec[x] = false;
		}
		else if (op == "check") {
			int x;
			cin >> x;
			if (vec[x]) cout << "1\n";
			else cout << "0\n";
		}
		else if (op == "toggle") {
			int x;
			cin >> x;
			vec[x] = !vec[x];
		}
		else if (op == "all") {
			vector<bool> newVec(21, true);
			vec.clear();
			vec.assign(newVec.begin(), newVec.end());
		}
		else if (op == "empty") {
			vector<bool> newVec(21, false);
			vec.clear();
			vec.assign(newVec.begin(), newVec.end());
		}
	}

	return 0;
}

profile
Be able to be vulnerable, in search of truth

0개의 댓글