백준 11723 집합

CJB_ny·2023년 1월 9일
0

백준

목록 보기
45/104
post-thumbnail

집합

일단 getline과 cin함수의 유의점만 생각하면 간단하게 풀렸던 문제인거 같다.

getline애서 계속 에러가 났었는데 이게

https://velog.io/@jxlhe46/C-getline-%ED%95%A8%EC%88%98

여기 읽어보면 된다.

cin은 버퍼에 개행문자를 그대로 남겨두어서

cin 다음에 바로 getline사용하면 개행문자가 getline에 입력받게 된다.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
#define endl "\n"

int n;
string m;
vector<int> vec;

void Add(int val)
{
	auto iter = find(vec.begin(), vec.end(), val);
	if (iter == vec.end())
		vec.push_back(val);
}

void Remove(int val)
{
	auto iter = find(vec.begin(), vec.end(), val);
	if (iter != vec.end())
		vec.erase(iter);

	int a = 10;
}

void Check(int val)
{
	auto iter = find(vec.begin(), vec.end(), val);
	if (iter == vec.end())
		cout << 0 << endl;
	else cout << 1 << endl;
}

void Toggle(int val)
{
	auto iter = find(vec.begin(), vec.end(), val);
	if (iter == vec.end())
		vec.push_back(val);
	else
		vec.erase(iter);
}

void All()
{
	vec.clear();
	for (int i = 0; i <= 20; ++i) 
	{
		vec.push_back(i);
	}
}

void Empty()
{
	vec.clear();
}

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

	cin >> n;
	cin.ignore();

	for (int i = 0; i < n; ++i)
	{
		getline(cin, m);
		string pre = m.substr(0, m.find(" "));
		string suf = m.substr(m.find(" ") + 1);
		int val = atoi(suf.c_str());
		
		if (pre == "add") Add(val);
		else if (pre == "remove") Remove(val);
		else if (pre == "check") Check(val);
		else if (pre == "toggle") Toggle(val);
		else if (pre == "all") All();
		else if (pre == "empty") Empty();
	}
	
	return 0;
}
profile
https://cjbworld.tistory.com/ <- 이사중

0개의 댓글