问题遇到的现象和发生背景
问题相关代码,请勿粘贴截图
C或C++
运行结果及报错内容
找出所以的湖景房
我的解答思路和尝试过的方法
我已经用伪代码的形式完成了一次
我想要达到的结果
C或者C++,并且程序尽量精妙一些
C或C++
找出所以的湖景房
我已经用伪代码的形式完成了一次
C或者C++,并且程序尽量精妙一些
使用队列的方法:
代码:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#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;
}
House gettop(Queue* q)
{
return q->a[q->base];
}
int main()
{
int a[MAXLEN], n, i;
Queue q;
init(&q);
printf("请输入n:");
scanf("%d", &n);
printf("请输入从西向东%d个房屋的高度:",n);
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;
int hmax=0;
printf("所有的海景房:");
while (!empty(q))
{
pop(&q, &hh);
if (hh.height > hmax)
{
printf("%d ", hh.id);
hmax = hh.height;
}
}
return 0;
}