问题 A: DS堆栈--逆序输出(STL栈使用)

问题 A: DS堆栈–逆序输出(STL栈使用)
时间限制: 1 Sec 内存限制: 128 MB

题目描述

C++中已经自带堆栈对象stack,无需编写堆栈操作的具体实现代码。

本题目主要帮助大家熟悉stack对象的使用,然后实现字符串的逆序输出

输入一个字符串,按字符按输入顺序压入堆栈,然后根据堆栈后进先出的特点,做逆序输出

stack类使用的参考代码

n包含头文件 : #include
n创建一个堆栈对象s(注意stack是模板类):stack s; //堆栈的数据类型是字符型
n把一个字符ct压入堆栈: s.push(ct);
n把栈顶元素弹出:s.pop();
n获取栈顶元素,放入变量c2: c2 = s.top();
n判断堆栈是否空: s.empty(),如果为空则函数返回true,如果不空则返回false
输入

第一行输入t,表示有t个测试实例
第二起,每一行输入一个字符串,注意字符串不要包含空格

字符串的输入可以考虑一下代码:

#include <string>

int main()

{ string str;

  Int len;

  cin>>str; //把输入的字符串保存在变量str中

  len = str.length()  //获取输入字符串的长度

}

输出

每行逆序输出每一个字符串
样例输入
2
abcdef
aabbcc
样例输出
fedcba
ccbbaa

#include<iostream>
#include<stack>
#include<map>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;

stack<char> p;

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		string str;
		int len;
		cin >> str;
		len = str.length();
		for (int i = 0; i < len; i++)
		{
			p.push(str[i]);
		}
		while (!p.empty())
		{
			cout << p.top();
			p.pop();
		}
		cout << endl;
	}
	return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值