【题解】CH1101 栈+vector

博客提到一个编程问题,将栈定义在外面时会少输出一个数,而把栈当成参数传递则不会出现该情况,作者对此感到困惑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接
把栈定义在外面会少输出一个数,当成参数传就不会,好迷啊

#include<cstdio>
#include<stack>
#include<vector>
using namespace std;
int n,cnt;
vector<int>vx;
void dfs(stack<int>sta,int step)
{
    if(cnt==20)return;
    if(step==n+1)
    {
        for(vector<int>::iterator it=vx.begin();it!=vx.end();it++)
            printf("%d",*it);
        while(!sta.empty())
        {
            printf("%d",sta.top());sta.pop();
        }
        putchar('\n');
        cnt++;
        return ;
    }
    if(!sta.empty())
    {
        vx.push_back(sta.top());sta.pop();
        dfs(sta,step);
        sta.push(*--vx.end());vx.pop_back();
    }
    sta.push(step);
    dfs(sta,step+1);
}
int main()
{
    scanf("%d",&n);
    stack<int>sta;
    dfs(sta,1);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值