上海网站空间,东京购物,托管竞价账户哪家好,河南省建设厅证件查询Description
C中已经自带堆栈对象stack#xff0c;无需编写堆栈操作的具体实现代码。
本题目主要帮助大家熟悉stack对象的使用#xff0c;然后实现字符串的逆序输出
输入一个字符串#xff0c;按字符按输入顺序压入堆栈#xff0c;然后根据堆栈后进先出的特点#xff0…Description
C中已经自带堆栈对象stack无需编写堆栈操作的具体实现代码。
本题目主要帮助大家熟悉stack对象的使用然后实现字符串的逆序输出
输入一个字符串按字符按输入顺序压入堆栈然后根据堆栈后进先出的特点做逆序输出
stack类使用的参考代码
包含头文件stack #include stack创建一个堆栈对象s注意stack是模板类stack char s; //堆栈的数据类型是字符型把一个字符ct压入堆栈s.push(ct);把栈顶元素弹出s.pop();获取栈顶元素放入变量c2c2 s.top();判断堆栈是否空s.empty()如果为空则函数返回true如果不空则返回false
Input
第一行输入t表示有t个测试实例 第二起每一行输入一个字符串注意字符串不包含空格
字符串的输入可以考虑以下代码
#include string
int main()
{ string str;int len;cin str; //把输入的字符串保存在变量str中len str.length() //获取输入字符串的长度
}
Output
每行逆序输出每一个字符串
Sample
Input
2
abcdef
aabbcc
Output
fedcba
ccbbaaAC代码
#include stack
#include iostream
#include cstring
using namespace std;
//答案仅供参考请勿直接复制粘贴
int main() {int t;cin t;string str;int len;while (t--) {stack char s;cin str;len str.length();for (int i 0; i len; i) {s.push(str[i]);}if (!s.empty()) {for (int i 0; i len; i) {cout s.top();s.pop();}cout endl;}}
}