PS/백준

[ 백준 ] 11723번 : 집합 - ( C++ / C )

발효홍삼 2022. 3. 5. 16:54
728x90
  • 문제

https://www.acmicpc.net/problem/11723

 

11723번: 집합

첫째 줄에 수행해야 하는 연산의 수 M (1 ≤ M ≤ 3,000,000)이 주어진다. 둘째 줄부터 M개의 줄에 수행해야 하는 연산이 한 줄에 하나씩 주어진다.

www.acmicpc.net


  • 풀이법 ( 알고리즘 )

간단히 S라는 이름의 배열을 선언하고 add 명령이 들어온 경우 값을 넣고, remove 명령이 들어온 경우 값을 지우며 각 명령에 따라 출력을 하는 등 구현을 하였다.


  • 풀이 - C++ ( C )
#include <iostream>
#include <cstring>//memset

using namespace std;

int S[22];
int m , x; // m : 수행해야 하는 연산의 수 , x : 연산 시 사용될 int 형 변수
string input; // input : 연산 명령어

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

	//input & solve
	cin >> m;

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

		if (input == "add")
		{
			cin >> x;
			S[x] = 1;
		}
		else if (input == "remove")
		{
			cin >> x;
			S[x] = 0;
		}
		else if (input == "check")
		{
			cin >> x;
			if (S[x])cout << "1\n";
			else cout << "0\n";
		}
		else if (input == "toggle")
		{
			cin >> x;
			if (S[x]) S[x] = 0;
			else S[x] = 1;
		}
		else if (input == "all")
		{
			for (int i = 1; i <= 20; i++)
				S[i] = 1;
		}
		else
		{
			for (int i = 1; i <= 20; i++)
				S[i] = 0;
		}
	}

	return 0;
}
728x90