单调栈

来源:互联网 发布:一淘和淘宝联盟一起用 编辑:程序博客网 时间:2024/04/25 15:25

给定一组数,做成单调递增栈(栈底到栈顶递增),一组数的选择会影响到这个程序

#include<stack>#include<iostream>#include<cstdio>using namespace std;int main(){    int a[10]={1,5,9,2,4,2,6,10},i;    stack<int>s;    while(!s.empty())    s.pop();    s.push(a[0]);    for(i=1;i<=7;i++)    {        while(a[i]<s.top()&&!s.empty())            s.pop();        s.push(a[i]);    }    while(!s.empty())    {        printf("%d ",s.top());        s.pop();    }    return 0;}


0 0
原创粉丝点击