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

  • 相关阅读:
    Docker入门尝鲜
    算法 |【实验5.2】1-深度优先搜索暴力求解旅行商问题
    VUE3学习小记(2)- ref 与 reactive
    code 网址
    Nvm管理NodeJs版本
    SAP UI5 FileUploader 控件实现本地文件上传,接收服务器端的响应时遇到跨域访问错误的试读版
    【MindSpore 入门教程】01 张量Tensor
    DBeaver连接clickhouse
    并发冲突:记一次导致流量放大的生产问题
    MediaBox助力企业一站式获取音视频能力
  • 原文地址:https://blog.csdn.net/iluo12/article/details/125430736