问题遇到的现象和发生背景
如图所示
问题相关代码,请勿粘贴截图
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;
}Stack;
void init(Stack* q)
{
q->fornt = -1;
}
void push(Stack* q, House d)
{
q->fornt++;
q->a[q->fornt] = d;
}
int pop(Stack* q, House* e)
{
if (q->fornt <=-1)
return 0;
*e = q->a[q->fornt];
q->fornt--;
return 1;
}
//判断是否为空
int empty(Stack q)
{
if (q.fornt <= -1)
return 1;
else
return 0;
}
int main()
{
int n, i;
Stack q;
init(&q);
printf("请输入n:");
scanf("%d", &n);
printf("请输入从西向东%d个房屋的高度:", n);
for (i = 0; i < n; i++)
{
House h;
scanf("%d", &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;
}