PAT Basic 1009

1009 说反话 (20 分)

给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。

输入格式:

测试输入包含一个测试用例,在一行内给出总长度不超过 80 的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用 1 个空格分开,输入保证句子末尾没有多余的空格。

输出格式:

每个测试用例的输出占一行,输出倒序后的句子。

输入样例:

Hello World Here I Come

输出样例:

Come I Here World Hello


这里用栈 vector都可以
本方法注意一点,输入5个单词,其实最后的i值为6,我老是格式错误,就是这个原因
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <vector>
#include<algorithm>
#include<string>
#include<math.h>
#define max 1000
#define debug 0
using namespace std;

int main() {
#if debug
    freopen("in.txt", "r", stdin);
#endif

    string words[max];
    int i = 0;
    while (cin >> words[i++]);
    for (int j = i-2; j>0; j--)
        cout << words[j]<<' ';
    cout << words[0] << endl;


#if debug
    freopen("CON", "r", stdin);
#endif
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/lxzbky/p/10498668.html