top1编程
← 返回题目
题解

【基础】相似数

1 条题解

  • 0
    @ 2026-7-28 23:26:20
    #include <bits/stdc++.h>  
    using namespace std;  
    int st[200005]; // 用数组模拟栈  
    int top = -1; // 栈顶索引  
    int n, x;  
    int main() {  
        cin >> n;  
        st[++top] = 0; // 初始化栈,先放入0  
        for (int i = 1; i <= n; i++) {  
            cin >> x; // 输入每个数字  
            // 模拟栈的pop操作  
            while (top != -1 && st[top] >= x) top--; // 栈不为空且栈顶更大  
            if (top != -1) {  
                cout << st[top] << " "; // 输出栈顶元素或0  
            } else {  
                cout << 0 << " ";  
            }  
            // x入栈  
            st[++top] = x; // 将x入栈  
        }  
        return 0;  
    }
    
    • 1