백준 11723 - 집합
(https://www.acmicpc.net/problem/11723)
처음에는 문제 이름부터 집합이니 그냥 set을 사용해야겠다고 생각했으나 시간초과가 났다. 그래서 원소가 20개이니, 배열을 만들어서 원소가 집합에 들어있으면 그 원소를 인덱스로 가지는 곳을 1로 만들고 들어있지 않다면 0으로 세팅했다.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int s[21]={0,};
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
char str[10];
scanf("%s",str);
if(strcmp(str,"add")==0)
{
int x;
scanf("%d",&x);
if(s[x]==0)
s[x]=1;
}
else if(strcmp(str,"remove")==0)
{
int x;
scanf("%d",&x);
if(s[x]==1)
s[x]=0;
}
else if(strcmp(str,"check")==0)
{
int x;
scanf("%d",&x);
printf("%d\n",s[x]);
}
else if(strcmp(str,"toggle")==0)
{
int x;
scanf("%d",&x);
s[x]=!s[x];
}
else if(strcmp(str,"all")==0)
{
for(int i=0;i<=20;i++)
{
s[i]=1;
}
}
else
{
memset(s,0,21*sizeof(int));
}
}
return 0;
}