标签:
队列和栈: #include<stdio.h> #include<string.h> #include<queue> #include<stack> # define N 10 using namespace std; int main () { int T, n, x, t; char s[N], str[N]; scanf("%d", &T); while (T--) { scanf("%d %s", &n, str); if (strcmp(str, "FIFO") == 0) { queue<int>q; while (n--) { scanf("%s", s); if (strcmp(s, "IN") == 0) { scanf("%d", &x); q.push(x); } else { if (!q.empty()) { t = q.front(); q.pop(); printf("%d\n", t); } else printf("None\n"); } } } //FIFO代表先进先出,运用队列做 else { stack<int>q; while (n--) { scanf("%s", s); if (strcmp(s, "IN") == 0) { scanf("%d", &x); q.push(x); } else { if (!q.empty()) { t = q.top(); q.pop(); printf("%d\n", t); } else printf("None\n"); } } } //FILO代表先进后出,运用栈做 } return 0; }
队列: #include<stdio.h> #include<queue> using namespace std; int main () { int a[5] = {1, 2, 4, 5, 6}, i, x; queue<int>q; for (i = 0; i < 5; i++) q.push(a[i]); while (!q.empty()) { x = q.front(); q.pop(); printf("%d ", x); } printf("\n"); return 0; } 优先队列: #include<stdio.h> #include<queue> using namespace std; typedef struct node { int x, step; friend bool operator < (node n1, node n2) { return n1.step > n2.step; } }NODE; int main () { NODE no[5] = { {1, 5}, {2, 2}, {3, 4}, {4, 3}, {1, 1} }, n; int i; priority_queue<NODE>q; for (i = 0; i < 5; i++) q.push(no[i]); while (!q.empty()) { n = q.top(); q.pop(); printf("%d %d\n", n.x, n.step); } return 0; } 栈: #include<stdio.h> #include<stack> using namespace std; int main () { int a[5] = {1, 3, 4, 5, 6}, i, x; stack<int>q; for (i = 0; i < 5; i++) q.push(a[i]); while (!q.empty()) { x = q.top(); q.pop(); printf("%d ", x); } printf("\n"); return 0; }
HDU 1702 ACboy needs your help again!(附加优先队列)
标签:
原文地址:http://www.cnblogs.com/syhandll/p/4451815.html