• 【牛客】HJ1 字符串最后一个单词的长度


    💒三行代码做一道题HJ1 字符串最后一个单词的长度

    我的意思是不包括固定代码哦!
    在这里插入图片描述

    🧸读题

    输出几个单词,以空格隔开,输出最后一个单词的长度。

    🧸代码

    直接写最终解题代码

    #include <iostream>
    using namespace std;
    int main()
    {
        string s;
        getline(cin,s);
        cout<<s.size()-s.rfind(' ')-1<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    🧸解读代码

    string s;
    创建string
    getline(cin,s);
    连续接收字符串
    cout<<s.size()-s.rfind(' ')-1<<endl;
    size减倒数第一个‘ ’(空格)的位置 再减去1,因为size是最后一个字符的下一个位置
    这样左开右闭相减就是 字符的个数。

    🧸代码迭代过程

    🌼第一代

    这是几个月前的记录

    #include <iostream>
    using namespace std;
     
    int main()
    {
        string s;
        //cin>>s;//cin读到空格或换行结束 scanf同理
        //方法一:一个字符一个字符拿
    //     char ch = getchar();
    //     //char ch = cin.get();
    //     while(ch!='\n')
    //     {
    //         s+=ch;
    //         ch = getchar();
    //     }
        //方式二:
        getline(cin,s);
        size_t pos = s.rfind(' ');
        if(pos == string::npos)
        {
            cout <<s.size()<<endl;
        }
        else{
            cout << s.size() - pos-1;
        }
        return 0;   
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    🌼第二代

    几个月后的今天我重新做了一下

    #include <iostream>
    using namespace std;
    
    int main()
    {
        string s;
        getline(cin,s);
        size_t pos = s.rfind(' ');
        int count  = 0;
        while(pos != s.size()-1)
        {
            ++pos;
            ++count;
        }
        cout <<count<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    🌼第三代

    我写完后,看了第一版的代码,于是觉得 while循环做的事,是冗余的!有了进一步的改造

    #include <iostream>
    using namespace std;
    
    int main()
    {
        string s;
        getline(cin,s);
        size_t pos = s.rfind(' ');
        cout<<s.size()-pos-1<<endl;
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    🌼最终版

    后来发现pos做的事也可以省略!!!

    #include <iostream>
    using namespace std;
     
    int main()
    {
        string s;
        getline(cin,s);
        cout<<s.size()-s.rfind(' ')-1<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    🌼其他大佬解法

    算最后一个单词的size
    那直接循环接收,覆盖掉之前的单词,直接输出size,牛蛙!

    int main() {
        string s;
        while(cin >> s);
        cout << s.size();
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    🌈加油,祝你早日拿到心仪的offer!

  • 相关阅读:
    JavaScript对象详解,js对象属性的添加
    springboot236基于springboot在线课程管理系统的设计与实现
    什么时候用C而不用C++?
    RocketMQ
    MySQL 索引
    C++函数重载
    中国移动物联网开放平台OneNET学习笔记(2)——设备接入测试(MQTT协议)OneNET Studio篇
    DL Homework 4
    elasticsearch分析插件 安装analysis-ik
    好朋友离职了,一周面试了20多场,我直呼内行
  • 原文地址:https://blog.csdn.net/iluo12/article/details/125430736