ARTICLE DETAIL

资讯详情

深耕网站视觉设计与运营推广的一线实战洞察。

【洛谷 B3614】【模板】栈

【洛谷 B3614】【模板】栈 目录题目解法一、栈题目解法一、栈#include stdio.h #include stdlib.h #include stdbool.h #include stdint.h #include inttypes.h /**********************************************************************/ #define STACK_CAP 1000000 struct stack { int m_top; uint64_t m_nodes[STACK_CAP]; void (*clear)(struct stack *); int (*size)(struct stack *); bool (*top)(struct stack *, uint64_t *); void (*push)(struct stack *, uint64_t); bool (*pop)(struct stack *); }; static bool _stack_is_empty(struct stack *self) { return self-m_top -1; } static void stack_clear(struct stack *self) { self-m_top -1; } static int stack_size(struct stack *self) { return self-m_top 1; } static bool stack_top(struct stack *self, uint64_t *val) { if (_stack_is_empty(self)) return false; *val self-m_nodes[self-m_top]; return true; } static void stack_push(struct stack *self, uint64_t val) { self-m_nodes[self-m_top] val; } static bool stack_pop(struct stack *self) { if (_stack_is_empty(self)) return false; self-m_top--; return true; } void stack_init(struct stack *stack) { stack-m_top -1; stack-clear stack_clear; stack-size stack_size; stack-top stack_top; stack-push stack_push; stack-pop stack_pop; } /**********************************************************************/ struct opt_info { int opt; uint64_t x; }; void read_num(int *num) { scanf(%d, num); } void read_opt_info(struct opt_info *info) { char buf[16]; scanf(%s, buf); switch (buf[0]) { case p: if (buf[1] u) { info-opt 1; scanf(% SCNu64, info-x); } else { info-opt 2; } break; case q: info-opt 3; break; case s: info-opt 4; break; default: info-opt -1; break; } } /**********************************************************************/ struct stack stack; int main(void) { int group_num; int opt_num; struct opt_info info; uint64_t ans; stack_init(stack); read_num(group_num); for (int i 0; i group_num; i) { read_num(opt_num); for (int j 0; j opt_num; j) { read_opt_info(info); switch (info.opt) { case 1: stack.push(stack, info.x); break; case 2: if (!stack.pop(stack)) printf(Empty\n); break; case 3: if (!stack.top(stack, ans)) printf(Anguei!\n); else printf(% PRIu64 \n, ans); break; case 4: ans stack.size(stack); printf(% PRIu64 \n, ans); break; default: break; } } stack.clear(stack); } return 0; }
返回列表