기본 콘텐츠로 건너뛰기

[BOJ 11723]

strcmp를 사용하기 위해서 <cstring> 추가
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
bool check(vector<int> vec,int x){
for (int i=0;i<vec.size();i++){
if (vec[i] == x)
return true;
}
return false;
}
void add(vector<int>& vec, int x){
if (check(vec,x)) return;
vec.push_back(x);
}
void erase(vector<int>& vec,int x){
for (int i=0;i<vec.size();i++){
if (vec[i] == x)
vec.erase(vec.begin()+i);
}
}
void toggle(vector<int>&vec, int x){
for (int i=0;i<vec.size();i++){
if (vec[i] == x){
vec.erase(vec.begin()+i);
return;
}
}
vec.push_back(x);
}
void all(vector<int>&vec){
vec.clear();
for (int i=1;i<=20;i++)
vec.push_back(i);
}
void empty(vector<int>&vec){
vec.clear();
}
int main(void){
int N;
scanf("%d",&N);
char ch;
int x;
vector<int> set;
getchar();
for (int i=0;i<N;i++){
int idx=0;
char cmd[10];
while ((ch = getchar()) != ' ' && ch != '\n'){
cmd[idx++] = ch;
}
cmd[idx] = NULL;
if (strcmp(cmd,"add") == 0){
scanf("%d",&x);
add(set, x);
getchar();
}
else if (strcmp(cmd,"remove") == 0){
scanf("%d",&x);
erase(set, x);
getchar();
}
else if (strcmp(cmd,"check") == 0){
scanf("%d",&x);
if (check(set, x))
printf("1\n");
else
printf("0\n");
getchar();
}
else if (strcmp(cmd,"toggle") == 0){
scanf("%d",&x);
toggle(set, x);
getchar();
}
else if (strcmp(cmd,"all") == 0){
all(set);
}
else if (strcmp(cmd,"empty") == 0){
empty(set);
}
}
}
view raw [BOJ 11723] hosted with ❤ by GitHub

댓글

이 블로그의 인기 게시물

Tree traversal의 3가지

1. 전위 순회 (Preorder Traversal) Root -> Left Tree -> Right Tree   ( 루트를 제일 처음에 방문 ) 2. 중위 순회 (Inorder Traversal) Left Tree -> Root -> Right Tree   ( 루트를 중간에 방문 ) 3. 후위 순회 (Postorder Traversal) Left Tree -> Right Tree -> Root   ( 루트를 제일 마지막에 방문 ) <소스코드> 출처 : http://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder /// C program for different tree traversals #include <stdio.h> #include <stdlib.h> /* A binary tree node has data, pointer to left child    and a pointer to right child */ struct node {      int data;      struct node* left;      struct node* right; }; /* Helper function that allocates a new node with the    given data and NULL left and right pointers. */ struct node* newNode(int data) {      struct node* node = (struct node*)                 ...