HDU 1062

来源:互联网 发布:python 培训 郑州 编辑:程序博客网 时间:2024/05/22 07:40

水了一道题,看到网上一些人的解题报告并不完美。这里用栈去模拟这个过程应该是比较好的解法,如果用判断空格,然后用 i-1的话,遇到以空格开头的数据就不对了。

#include <cstdio>#include <iostream>#include <cstring>#include <stack>using namespace std;int main() {int t;scanf("%d", &t);getchar();for(int k = 0; k < t; k++) {char str[1002];stack<char> s;int index = 0;gets(str);for(int i = 0; str[i] != '\0'; i++) {if(str[i] != ' ') {s.push(str[i]);}else {while(!s.empty()) {printf("%c", s.top());s.pop();}printf(" ");}}while(!s.empty()) {printf("%c", s.top());s.pop();}printf("\n");}return 0;}


原创粉丝点击