1 条题解

  • 1
    @ 2025-10-25 11:33:25
    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
    	string sentence;  // 定义一个字符串变量,用来存储输入的英文句子
        string words[50]; // 定义一个字符串数组,用来存储分割后的单词(最多50个)
        int wordCount = 0; // 计数器,记录有多少个单词
        
        // 读取一整行英文句子
        getline(cin, sentence);
        
        // 第一步:把句子分割成单词
        string currentWord = ""; // 用来临时存储当前正在读取的单词
        
        // 遍历句子中的每一个字符
        for(int i = 0; i < sentence.length(); i++) {
            // 如果遇到空格,说明一个单词结束了
            if(sentence[i] == ' ') {
                // 把当前单词保存到数组中
                words[wordCount] = currentWord;
                wordCount++;  // 单词数量加1
                currentWord = ""; // 清空,准备读取下一个单词
            } else {
                // 如果不是空格,就把这个字符加到当前单词后面
                currentWord += sentence[i];
            }
        }
        
        // 不要忘记最后一个单词(因为句子末尾没有空格)
        words[wordCount] = currentWord;
        wordCount++;
        
        // 第二步:倒序输出单词
        // 从最后一个单词开始,倒着往前输出
        for(int i = wordCount - 1; i >= 0; i--) {
            cout << words[i];
            // 如果不是最后一个单词,就输出一个空格
            if(i > 0) {
                cout << " ";
            }
        }
        
        return 0;
    }
    
    • 1

    信息

    ID
    338
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    5
    已通过
    3
    上传者