字符串反转,栈模拟(ZOJ1151)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=151

这里可以用栈模拟,也可以用STL,reverse();函数。

但是我这里用栈模拟,PE了,了解一下这个做法吧。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <string>

using namespace std;

int main()
{
    int n;
    cin >> n;
    while (n--)
    {
        int t,j=1;
        char k;
        cin >> t;
        getchar();
        while (1)
        {
            if (j > t)
                break;
            string p;
            cin >> p;
            reverse(p.begin(), p.end());
            cout << p;
            k = getchar();
            if (k ==' ')
                cout <<" ";
            if (k == '
')
            {
                j++;
                cout << endl;
            }
        }
        if (n)
            cout << endl;
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/TreeDream/p/5414172.html