• 【牛客】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!

  • 相关阅读:
    鸿蒙OS初识
    SQL 练习
    CentOS7安装Xrdp以便Windows远程桌面连接
    springboot2.X整合mybatis使用joda时间格式变量完成插入操作
    【见闻录系列】我所理解的搜索业务二三事
    SpringBean的生命周期
    数组转树形结构
    拼多多api接口应用示例
    MongoDB基础操作--基础工具使用
    雷军的开源情怀
  • 原文地址:https://blog.csdn.net/iluo12/article/details/125430736