Hdu1062题(英文题,用栈的思想解决)
**题目链接:**http://acm.hdu.edu.cn/showproblem.php?pid=1062思路:
利用栈来进行存取字母。当读取到空格或者是换行的时候,就意味着需要将之前存入栈的值打印出来了。在此之外的情况的话就是将字母存入栈内。
AC的代码
#include<stdio.h>
#define MAX 1001
char stack[MAX];
int pIndex;
void push(char c){
stack[pIndex++] = c;//先进行pindex的下标赋值,后再将pindex加1;
}
char pop(){
return stack[--pIndex];//先将pIndex减1,然后再返回减1后的stack的字符值。
}
int main(){
int T;
char c;
scanf("%d",&T);
getchar();
while(T--){
pIndex = 0;//每个读取前将栈的初始位置为0
while(1){
c = getchar();
if(c == ' '||c == '\n'){
while(pIndex){
putchar(pop());//将栈内的值全部输出清空
}
putchar(c);//输出读入的空格或者换行
}else{//不是空格和换行的时候就是读入字符 放入栈中
push(c);
}
if(c == '\n'){//如果是换行就进入下个测试数据
break;
}
}
}
return 0;
}