AB7. 【模板】队列
描述
输入描述
输出描述
示例1
输入:
6 push 1 pop front push 2 push 3 pop
输出:
1 error 2
C 解法, 执行用时: 12ms, 内存消耗: 1548KB, 提交时间: 2022-07-06
#include<stdio.h> #include<stdlib.h> #include<string.h> int a[100000]; char s[20]; int fr=0; int re=0; void _push(); void _pop(); void _front(); int main() { int n,i; scanf("%d",&n); getchar(); for (i=0;i<n;i++) { gets(s); if(s[1]=='u') _push(); else if(s[1]=='o') _pop(); else if(s[1]=='r') _front(); } } void _push() { int sl=strlen(s); char sr[10]; strcpy(sr,s+5); int n=atoi(sr); a[re++]=n; //printf("%d %d\n",t,n); } void _pop() { if(fr==re) { printf("error\n"); return; } int sl=strlen(s); char sr[10]; strcpy(sr,s+4); int n=atoi(sr); printf("%d\n",a[fr++]); } void _front() { if(fr==re) { printf("error\n"); return; } printf("%d\n",a[fr]); }
C 解法, 执行用时: 14ms, 内存消耗: 780KB, 提交时间: 2022-04-17
#include <stdio.h> #include <stdlib.h> #include <string.h> void queue_opt(int *queue, int *pos, const char *buf) { int u; if(strncmp(buf, "push", 4) == 0) { u = atoi(buf+5); queue[pos[1]++] = u; } else if(strncmp(buf, "pop", 3) == 0) { if(pos[1] - pos[0] == 0) { printf("error\n"); } else { printf("%d\n", queue[pos[0]++]); } } else if(strncmp(buf, "front", 5) == 0) { if(pos[1] - pos[0] == 0) { printf("error\n"); } else { printf("%d\n", queue[pos[0]]); } } } int main() { char buf[128]; int a[100*1000]; int n; int pos[2] = {0, 0}; int i; scanf("%d\n", &n); if(n < 1 || n > 100*1000) { return -1; } for(i=0; i<n; i++) { memset(buf, 0x00, sizeof(buf)); fgets(buf, 127, stdin); queue_opt(a, pos, buf); } return 0; }
C 解法, 执行用时: 15ms, 内存消耗: 776KB, 提交时间: 2022-04-24
#include <stdio.h> #include <stdlib.h> #include <string.h> static int num[100000] = {-1}; static int start = 0; static int end = -1; void judge(char *arr) { int i, j = 0; int m = 0; char a[1000]; int len = strlen(arr); memset(a, 0, sizeof(char) * 1000); if (strncmp(arr, "push", 2) == 0) { strcpy(a, arr+5); m = atoi(a); end++; num[end] = m; } else if (strncmp(arr, "pop", 2) == 0) { if (num[start] == -1 || start > end) { printf("error\n"); return; } printf("%d\n", num[start]); num[start] = -1; start++; } else if (strncmp(arr, "front", 2) == 0) { if (num[start] == -1 || start > end) { printf("error\n"); return; } printf("%d\n", num[start]); } } int main(void) { int i, j = 0; int m, n = 0; char arr[1000]; memset(arr, 0, sizeof(char) * 1000); scanf("%d\n", &n); //printf("-test-%d\n",n); for (i = 0; i < n; i++) { gets(arr); judge(arr); } return 0; }
C++ 解法, 执行用时: 15ms, 内存消耗: 1660KB, 提交时间: 2022-04-17
#include <stdio.h> #include <stdlib.h> #include <string.h> void queue_opt(int *queue, int *pos, const char *buf) { int u; if(strncmp(buf, "push", 4) == 0) { u = atoi(buf+5); queue[pos[1]++] = u; } else if(strncmp(buf, "pop", 3) == 0) { if(pos[1] - pos[0] == 0) { printf("error\n"); } else { printf("%d\n", queue[pos[0]++]); } } else if(strncmp(buf, "front", 5) == 0) { if(pos[1] - pos[0] == 0) { printf("error\n"); } else { printf("%d\n", queue[pos[0]]); } } } int main() { char buf[128]; int a[100*1000]; int n; int pos[2] = {0, 0}; int i; scanf("%d\n", &n); if(n < 1 || n > 100*1000) { return -1; } for(i=0; i<n; i++) { memset(buf, 0x00, sizeof(buf)); fgets(buf, 127, stdin); queue_opt(a, pos, buf); } return 0; }
C++ 解法, 执行用时: 16ms, 内存消耗: 904KB, 提交时间: 2022-06-09
#include <stdio.h> #include <stdlib.h> #include <string.h> void queue_opt(int* queue, int* pos, const char* buf) { int u; if (strncmp(buf, "push", 4) == 0) { u = atoi(buf + 5); queue[pos[1]++] = u; } else if (strncmp(buf, "pop", 3) == 0) { if (pos[1] - pos[0] == 0) { printf("error\n"); } else { printf("%d\n", queue[pos[0]++]); } } else if (strncmp(buf, "front", 5) == 0) { if (pos[1] - pos[0] == 0) { printf("error\n"); } else { printf("%d\n", queue[pos[0]]); } } } int main() { char buf[128]; int a[100 * 1000]; int n; int pos[2] = {0, 0}; int i; scanf("%d\n", &n); if (n < 1 || n > 100 * 1000) { return -1; } for (i = 0; i < n; i++) { memset(buf, 0x00, sizeof(buf)); fgets(buf, 127, stdin); queue_opt(a, pos, buf); } return 0; }