elttiL moT nwod eht teerts sllac ruo god " ehT peek god " . piZ si a peehs god . tuB nehw moT seirt ot yas " peeS " , ti semoc tuo " peek " . dnA ni a yaw moT si thgir . piZ si syawla gnignirb sgniht oh rof su ot peek ! ll'I llet uoy tuoba emos fo meht .
s'piZ tsrif tneserp saw a eohs . tI saw edam fo neerg klis .
eW t'ndid wonk woh piZ dnuof eht eohs . tuB retfa a tnemom yraM , ym gib retsis , dlot em eht eohs dah a egnarts llems . I deddon dna dleh ym eson . " tahW od uoy kniht ti si ? "
" tI sllems ekil gnihtemos rof gninaelc . I kniht enoemos deirt ot naelc a tops ffo eht eohs . nehT eh tup ti ta eht rood ot yrd . "
" gnolA emac piZ . dnA eyb-doog eohs ! " I dias . " eW dluohs ekat ti kcab . "
" eW t'nac " . dias ym rettsis .
" ebyaM elttil moT si thgir , " yraM dias . " ebyaM piZ si a peek god ! "
你正在做英语阅读,可哪知这是一篇咸鱼文章,整个文章的所有单词都是翻转的,你很慌。
不过你是咸鱼程序员,你可以写代码将这篇文章翻转回来,那么翻转回来吧。
思路:用栈来做这道题,遇到字母,我们就扔到栈里面去,遇到空格或者换行的时候,我们就把栈里面的元素输出,然后再输出空格/换行就好了。
ps:不用考虑输入终止
代码:
#include<cstdio>
#include<cstring>
#include<stack>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
char a[100005];
char b[100005];
int main()
{
int L=0;//L记录总长
while(gets(a))
{
int p=0;
int len=strlen(a);
for(int i=L; i<L+len; i++)
b[i]=a[p++];
b[L+len]='\n';
L=L+len+1;
}
stack<char>q;
for(int i=0; i<L; i++)
{
if((b[i]>='a'&&b[i]<='z')||(b[i]>='A'&&b[i]<='Z'))
q.push(b[i]);
else
{
while(!q.empty())
{
printf("%c",q.top());
q.pop();
}
printf("%c",b[i]);
}
}
return 0;
}