본문 바로가기

Programming/Algorithm 193

백준 회전하는 큐 /*회전하는 큐라. 한번 문제를 읽어보고 풀어보도록 하겠습니다. 복습의 개념이 아니라,한번 풀어보도록 하겠습니다. 현재시간 5시 13분입니다. 3가지 연산이 가능합니다.첫번째 원소를 뽑아내는 것, 왼쪽으로 한 칸 이동하는 것 팝프론트해서 백하면 되겠습니다.오른쪽에 한 칸 이동하는 것은, 팝 백 해서 프론트하면 되겠습니다.포함되어 있는 수 ㅔㄴ이 주어지고 뽑아내려고 하는 원소의 위치가 주어집니다.처음 큐에서의 위치입니따. 2번 3번 연산의 최솟값을 출력하는 프로그램을 작성하세요 주어진 순서대로 뽑아내는데 드는 2번 3번 연산의최솟값을 출력하는 프로그램.*/ #include #include using namespace std;int n, r;deque deq;deque::iterator iter; int m.. 2018. 1. 29.
백준 디큐 /**/ #include #include #include using namespace std; int main() {int N;int num;string str;deque deq;cin >> N;for (int i = 0; i > str;if (str == "push_back") {cin >> num;deq.push_back(num);}else if (str == "push_front") {cin >> num;deq.push_front(num);}else if (str == "front") {if (!deq.empty()) {cout 2018. 1. 29.
백준 조세퍼스 문제 /*1번부터 엔번까지 엔명의 사람이 원을 이루어면서 앉아있고, 양의 정수 엠이 주어집니다. 순서대로 엠번쨰 사람을 제거합니다. 에명의 사람이 모두 제거될 때 까지 계속됩니다. 엔 엠 조세퍼스 수녕ㄹ이라고 합니다.7 3 조세퍼스 순열은 1234567 오께이 문제 이해했습니다. 어떻게 푸는지 살펴보도록 하겠습니다.*/ #include #include using namespace std; void Josepus(int N, int M) {queue q;for (int i = 1; i M;Josepus(N, M);return 0;} 대중화된 문제라던지, 풀이가 많은 것들, 그리고 문제로 주어진 것들에 대해서는 100퍼센트 완벽하게 풀이가 가능해야 됩니다. 그래야 코딩테스트 뿐만 아니라 면접때도 문제풀이를 통과할.. 2018. 1. 29.
백준 큐 1966 #include #include #include using namespace std;int t, n, m, x, r;int main() { scanf("%d", &t); while (t--) { r = 0; queue qu; priority_queue pq; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++) { scanf("%d", &x); qu.push({ x,i }); pq.push(x); } while (qu.size()) { int here = qu.front().first; int num = qu.front().second; qu.pop(); if (pq.top() == here) { r++; pq.pop(); if (num == m)break; } el.. 2018. 1. 29.
백준 DFS, BFS /*DFS, BFS 가 왜 큐문제에 들어있습니꽈그래프를 DFS 로 탐색한 결과와 BFS 로 탐색한 결과를 출력하는 프로그램을 작성해보도록 하겠스니다. 정점 번호가 작은 것을 먼저 방문하고, 더 이ㅏㅇ 방문할 수 있는 점이 없는 경우종료합니다. 정점 번호정점의 개수, 간선의 개수 ㅏㅁ색을 시작할 정점의 번호가 주어집니다. 입력으로 주어지는 간선은 양방향입니다. 1 2 3 41 1 1 12 1 13 1 14 1 1 1 이렇게 되어 있고 스타트는 1에서 한다? 1,2 에서 한다이것인가요. */ #include #include using namespace std; int Graph[1001][1001] = { 0 };int DFSvisit[1001] = { 0 };int BFSvisit[1001] = { 0 }.. 2018. 1. 29.
백준 큐 /*zbfmf rngusgks ekdma audfudzbfmf sjgsms rjt tkdlwm dpaxl vmfhsxm qor dhzpdldy.*/ #include #include #include using namespace std; int main(void) {int N;int num;string str;queue que;cin >> N;for (int i = 0; i > str;if (str == "push") {cin >> num;que.push(num);} else if (str == "front") {if (!que.empty()) {cout 2018. 1. 29.
백준 괄호 2504 #include #include #include using namespace std;int main() {string n;cin >> n;stack s;int ans = 0, k = 0, l = 0, mul = 1;for (int i = 0; i < n.length(); i++) {char now = n[i];switch(now) {case '(' :++k;s.push(now);mul *= 2;if (i + 1 < n.length() && n[i + 1] == ')')ans += mul;break;case '[' :++l;s.push(now);mul *= 3;if (i + 1 < n.length() && n[i + 1] == ']')ans += mul;break;case ')' :--k;s.pop();m.. 2018. 1. 29.
백준 스택 괄호 #include #include #include using namespace std; bool Check(string str) {int len = (int)str.length();stack st; for (int i = 0; i > n;for (int i = 0; i > str; if (Check(str)) {cout 2018. 1. 29.
백준 스택 사용 /*wkfyfmf sjgsms vntldlqrndhk wkfyffm Qhqsms vkq dlqrnrk rkxdk wpdlf ajswj emfdjrks wkfyrk wpdlf skwnddp skdhsms tmxordp vntlgksms tnstjsms qksemtl dh반드시 오름차순을 지키도록 한다고 ㅏㅂ니다. 임의의 수열이 주어졌을 때 스택을 이용해 그 수열을 만들 수 있는지없는 지 있따면 어떤 수넛로 푸시와 팝을 연산을 수행해야 하는지 알아낼 수 있습니다. 지금 넣었다고 가정하는 겁니까? 4 3 6 8 7 5 2 1 8 6 3 4*/ #include #include using namespace std; int main() {int stack[100000];char res[200000];int i, n.. 2018. 1. 29.
백준 스택 /*오케이 이렇게 하나씩 풀어나가면 됩니다. 정수를 저장하는 스택을 구현한 다음,입력으로 주어지는 명령을 처리하는 프로그램을 작성하세요.푸시 팝, 사이즈 엠티, 탑 스택의 가장 위에 있는 정수를 출력하세요.팝이랑 탑이랑 구분할 필요가 있ㅅ습니다.첫째 줄에 주어지는 명령의 수가 주어집니다. 스택에 대해서 모르니 이것은 정답을 보고풀어보도록 하겠습니다. */ #include #include #include using namespace std; int main(void) {int n;cin >> n; stack st;string str; for (int i = 0; i > str;if (str == "push") {int num;cin >> num;st.push(num);}else.. 2018. 1. 29.