之前接单做了这个湖景房问题,感到非常有趣,上传到csdn记录下来。
#include#include #define MAXLEN 100 typedef struct _houseinfo { int id; int height; }House; typedef struct _queue { House a[MAXLEN]; int fornt; int base; }Queue; //初始化队列 void init(Queue* q) { q->base = 0; q->fornt = -1; } //入队 void push(Queue* q, House d) { q->fornt++; q->a[q->fornt] = d; } //出队 int pop(Queue* q, House* e) { if (q->fornt < q->base) return 0; *e = q->a[q->base]; q->base++; return 1; } //判断是否为空 int empty(Queue q) { if (q.base > q.fornt) return 1; else return 0; } int main() { int n, i; int hmax=0; Queue q; init(&q); printf("请输入n:"); scanf("%d", &n); printf("请输入从东向西%d个房屋的高度:",n); int* a = (int*)malloc(n * sizeof(int)); for (i = 0; i < n; i++) scanf("%d", &a[i]); //入队 for (i = n - 1; i >= 0; i--) { House h; h.height = a[i]; h.id = i + 1; push(&q, h); } //第一个出队 House hh; printf("所有的海景房:n"); while (!empty(q)) { pop(&q, &hh); if (hh.height > hmax) { printf("第%d栋,房高%d米n", hh.id,hh.height); hmax = hh.height; } } return 0; }



