原题链接:https://vjudge.net/problem/UVA-514
分类:栈
备注:水题
前言:回顾的时候以为很水,但没有想象的那么水,实现起来还是有点麻烦的,再多看看作者的代码,思考一下怎么写出优美的代码吧。
代码如下:
#include<cstdio>
#include<stack>
using namespace std;
const int maxn = 1000 + 5;
int N;
bool solve()
{
int a[maxn], cnt = 1, x = 1;
stack<int>tmp;
for (int i = 1; i <= N; i++)
{
scanf("%d", &a[i]);
if (!a[i])return false;
}
while (x <= N)
{
while ((tmp.empty() || tmp.top() != a[cnt]) && x <= N)
tmp.push(x++);
while (!tmp.empty() && tmp.top() == a[cnt])
tmp.pop(), cnt++;
}
if (tmp.empty())printf("Yes\n");
else printf("No\n");
return true;
}
int main(void)
{
while (scanf("%d", &N) == 1 && N)
{
while (solve());
printf("\n");
}
return 0;
}
本文解析了UVA-514题目,通过使用栈数据结构来解决一个看似简单但实际上具有一定实现复杂度的问题。文章分享了代码实现细节,并鼓励读者深入理解并优化代码。
&spm=1001.2101.3001.5002&articleId=104430660&d=1&t=3&u=bb588f2b8b914d629b30866869596279)
699

被折叠的 条评论
为什么被折叠?



