1266:Reverse Number

来源:互联网 发布:网络直播的意义 编辑:程序博客网 时间:2024/05/29 02:22

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1266

方法:栈实现

思路:反转字符串首先想到了用栈实现,c++stl中就可以用stack实现,首先要包含<satck>,然后声明一个堆栈stack <数据类型 > s,堆栈有一些常用操作,比如pop()弹出栈顶元素,但是不返回栈顶元素,top()访问栈顶元素,经常与pop()配合使用,push()压入栈,empty()检查是否为空,size()求出栈的大小。当然对于这一类简单问题,也可以用数组来模拟栈。

难点:栈的使用


#include <cstdio>#include <string.h>#include <stack>using namespace std;int main(){int n;char str[110];stack <char> s;while(~scanf("%d",&n)){while(n--){            scanf("%s",&str);int len = strlen(str);for(int i = 0;i < len;i++){if(str[i]>='1'&&str[i]<='9'){s.push(str[i]);}}for(int i = 0;i < len;i++){if(str[i]>='1'&&str[i]<='9'){printf("%c",s.top());s.pop();}else{printf("%c",str[i]);}}printf("\n");}}return 0;}



0 0
原创粉丝点击