[algorithm] 백준 10866번 덱
·
algorithm/덱
스택,큐의 기본문제들과 같은 패턴으로덱을 잘 이해했는 지에 대한 개념확인 문제다#include #include #include using namespace std;int main() { ios::sync_with_stdio(0); cin.tie(0); int n,x; string s; cin >> n; deque d; for(int tmp=0; tmp> s; if(s == "push_front") { cin >> x; d.push_front(x); } if(s == "push_back") { ..
[algorithm] 백준 2164번 카드2
·
algorithm/큐
1부터 N까지의 원소가 담긴 큐를 설정하고큐의 남은 원소가 1개가될때까지홀수 번째 때 맨 앞 원소를 버리고 짝수 번째 때 맨 앞 원소를 맨 뒤로 옮기는 작업을 반복하므로count 변수를 설정해서count가 짝수일 때와 홀수일 때로 나눠서 풀었다 #include #include using namespace std;int main() { int n; cin >> n; int count = 1; int p; queue q; for(int tmp=1; tmp 홀수번째 때와 짝수번째 때로 굳이 나누지 않고두 작업을 한번의 작업으로 생각해서 푸는 방법도 있었다// Authored by : OceanShape// Co-authored by : -// http://boj.kr/d..
[algorithm] 백준 18258번 큐 2
·
algorithm/큐
백준 15845번 문제와 내용은 같지만차이는 입력 값의 크기와 시간 제한 차이이다 간단하게 풀 수 있는 방법은15845번 해답에ios::sync_with_stdio(false);cin.tie(NULL); 이 두 줄을 main함수 맨 위에 추가하는 것이다 ios::sync_with_stdio(false)의 의미는c의 printf/scanf와 c++의 cin/cout의 동기화를 해제하는 명령으로사용 시 printf/scanf를 사용하면 안된다 cin.tie(NULL)은 간단히 말해서입력 명령 전에 출력 버퍼를 비우지 않도록 하는 명령이다온라인 저지 사이트같은 곳에서는 입력과 출력이콘솔 상에 보이는 순서가 중요하지 않기에굳이 입력 전 출력 버퍼를 비우지 않아도 된다는 의미다. #include #include ..
[algorithm] 백준 10845번 큐
·
algorithm/큐
큐를 잘 이해하고 있는지 확인하는기본 문제다 구현 상의 특징이라고 할만한 건push X를 구현할 때push라는 단어가 입력되었을 때x를 입력하도록 구현해주면 된다cin은 공백을 기준으로 입력을 받기 때문이다 #include #include #include using namespace std;int main() { int n; int x; cin >> n; string s; queue q; for(int tmp=0; tmp> s; if(s == "push") { cin >> x; q.push(x); } if(s == "pop") ..
[algorithm] 백준 10773번 제로
·
algorithm/스택
#include #include #include using namespace std;int main() { ios::sync_with_stdio(0); cin.tie(0); int n; int x; int s; int res=0; cin >> n; stack st; for(int tmp=0; tmp> x; if(x == 0) { st.pop(); } else { st.push(x); } } while(st.size() != 0) { ..
[algorithm] 백준 10828번 스택
·
algorithm/스택
스택 개념 이해 확인을 위한기본 문제이다#include #include #include using namespace std;int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; string s; stack st; for(int tmp=0; tmp> s; if(s == "push") { int x; cin >> x; st.push(x); } if(s == "pop") { if(st.size() ..
[algorithm] 백준 1158번 요세푸스 문제
·
algorithm/연결 리스트
#include #include using namespace std;int main() { int n,k; int cnt = 0; cin >> n >> k; list l; list res; for(int tmp=0; tmp::iterator p = l.begin(); while(l.size() != 1) { cnt = k; while((cnt-1) != 0) { p++; if(p == l.end()) p = l.begin(); cnt--; } ..
[algorithm] 백준 5397번 키로거
·
algorithm/연결 리스트
처음에 문제를 풀 때리스트에 입력 문자열을 넣고리스트를 순회하면서 반복자를 리스트에 두고 각 문자들을 확인하여 vector에 반영하는 식으로 하려했다 하지만 이 경우벡터는 반복자를 사용할 수 있지만중간 삽입 삭제가 O(n)이여서 비효율적이다 결국 결과 출력할 리스트에서 반복자를 두고 조작하는 방식이 효율적이다또한 그렇게되면 리스트 2개가 아닌 1개만 사용해도 된다#include #include #include using namespace std;int main() { int n; string s; list l; cin >> n; for(int tmp=0; tmp> s; auto p = l.begin(); for(int i=0; i') ..
[algorithm] 백준 1406번 에디터
·
algorithm/연결 리스트
연결리스트의 개념을 잘 숙지했는지판단하기 위한 기본 문제다 #include #include #include using namespace std;int main() { string str; cin >> str; int n; cin >> n; list l; for(char c : str) l.push_back(c); char cm; list::iterator cursor = l.end(); for(int tmp=0; tmp> cm; if(cm == 'L') { if(cursor != l.begin()) cursor--; } if(cm == 'D') ..
[algorithm] 백준 1919번 애너그램 만들기
·
algorithm/배열
#include #include #include using namespace std;int main() { int cnt=0; string a,b; int result = 0; cin >> a >> b; for(int i : a) { for(int j : b) { if(i == j) cnt++; } if(cnt == 0) result++; else cnt = 0; } for(int i : b) { for(int j : a) { ..