题解 | 编排字符串 栈(备份栈)

编排字符串

https://www.nowcoder.com/practice/42c0673f04b34f66ae236a1cb7995532

#include <stdio.h>
#include <stack>
#include <string>
using namespace std;

int main() {
    stack<string> myStack; // stack<char[20]>这样错误
    int m;
    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
        char strArr[200] = { 0 };  // c语言字符数组
        scanf("%s", strArr); // 先读入字符数组
        string str = strArr; // 再赋值给string
        myStack.push(str);

        stack<string> outputStack = myStack;  // 备份栈
        for (int j = 1; j <= 4; j++) {
            if (outputStack.empty()) {
                break;
            }
            // c语言必须使用 c_str() 函数 才能输出字符串
            printf("%d=%s ", j, outputStack.top().c_str());
            outputStack.pop();
        }
        printf("\n");
    }
    return 0;
}

#考研##复试练习#
2025考研复试 文章被收录于专栏

复试ing,努力中。。。

全部评论

相关推荐

07-17 11:27
门头沟学院 Java
点赞 评论 收藏
分享
06-28 22:48
已编辑
广东金融学院 Java
小浪_Coding:学院本+这俩项目不是buff叠满了嘛
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务