问题:给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。
输入格式:
测试输入包含一个测试用例,在一行内给出总长度不超过500 000的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用若干个空格分开。
输出格式:
每个测试用例的输出占一行,输出倒序后的句子,并且保证单词间只有1个空格。
输入样例:
Hello World Here I Come
输出样例:
Come I Here World Hello
#include
#include
#define MAX 500010
using namespace std;
int main()
{
void print(int start, int endd, char str[]);
char str[MAX];
char ch;
int i = 0;
int j = 0;
int k = 0;
int start[50];
int end[50];
bool tmp = true;
while((ch=getchar())!='\n')
{
if(ch!=' '&&tmp)
{
str[i]=ch;
start[j]=i;
i++;
j++;
tmp = false;
}
else if(ch!=' '&&!tmp)
{
str[i]=ch;
i++;
}
else if(ch==' '&&tmp)
{
continue;
}
else if(ch==' '&&!tmp)
{
end[k]=i-1;
//i++;
k++;
tmp=true;
}
}
end[j-1]=i-1;
for(int i=j-1;i>0;i--){
print(start[i], end[i], str);
}
for(int i=start[0];i<=end[0];i++){
printf("%c",str[i]);
}
printf("\n");
return 0;
}
void print(int start, int endd, char str[])
{
for(int i=start;i<=endd;i++){
printf("%c",str[i]);
}
printf(" ");
}