当前位置:网站首页>【牛客】HJ1 字符串最后一个单词的长度
【牛客】HJ1 字符串最后一个单词的长度
2022-06-24 07:07:00 【Uaena_An】
三行代码做一道题HJ1 字符串最后一个单词的长度
我的意思是不包括固定代码哦!
🧸读题
输出几个单词,以空格隔开,输出最后一个单词的长度。
🧸代码
直接写最终解题代码
#include <iostream>
using namespace std;
int main()
{
string s;
getline(cin,s);
cout<<s.size()-s.rfind(' ')-1<<endl;
return 0;
}
🧸解读代码
string s;
创建stringgetline(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;
}
第二代
几个月后的今天我重新做了一下
#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;
}
第三代
我写完后,看了第一版的代码,于是觉得 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;
}
最终版
后来发现pos做的事也可以省略!!!
#include <iostream>
using namespace std;
int main()
{
string s;
getline(cin,s);
cout<<s.size()-s.rfind(' ')-1<<endl;
return 0;
}
其他大佬解法
算最后一个单词的size
那直接循环接收,覆盖掉之前的单词,直接输出size,牛蛙!
int main() {
string s;
while(cin >> s);
cout << s.size();
return 0;
}
加油,祝你早日拿到心仪的offer!
边栏推荐
- Wan Weiwei, a researcher from Osaka University, Japan, introduced the rapid integration method and application of robot based on WRS system
- 更改SSH端口号
- rpiplay实现树莓派AirPlay投屏器
- Database to query the quantity of books lent in this month. If it is higher than 10, it will display "more than 10 books lent in this month". Otherwise, it will display "less than 10 books lent in thi
- MySQL | 视图《康师傅MySQL从入门到高级》笔记
- 不能改变虚拟机电源状态报错解决方案
- [team management] 25 tips for testing team performance management
- 数据库迁移从PostgreSQL迁移到 MYSQL
- IDEA另起一行快捷键
- 一文详解|增长那些事儿
猜你喜欢
![打印出来的对象是[object object],解决方法](/img/fc/9199e26b827a1c6304fcd250f2301e.png)
打印出来的对象是[object object],解决方法

数据中台:数据采集和抽取的技术栈详解

ZUCC_ Principles of compiling language and compilation_ Experiment 0607 grammar analysis ll analysis

Base64编码详解及其变种(解决加号在URL变空格问题)

WebRTC系列-网络传输之5选择最优connection切换

Background management of uniapp hot update

Database migration from PostgreSQL to MySQL

关于ETL看这篇文章就够了,三分钟让你明白什么是ETL

opencv最大值滤波(不局限于图像)

every()、map()、forEarch()方法。数组里面有对象的情况
随机推荐
MyCAT读写分离与MySQL主从同步
关于ETL看这篇文章就够了,三分钟让你明白什么是ETL
原生小程序用画布制作海报,等比例缩放,和uniapp差不多就是写法有点不同
基于单片机开发的酒精浓度测试仪方案
Shell pass parameters
GradScaler MaxClipGradScaler
liunx服务器 telnet 带用户名 端口登陆方法
Ordering of MySQL composite index
Liunx Mysql安装
Liunx change the port number of vsftpd
利用ngrok做内网穿透
【MySQL从入门到精通】【高级篇】(一)字符集的修改与底层原理
mysql写的代码数据 增删查改等等
1528. 重新排列字符串
工具类
Analyze the meaning of Internet advertising terms CPM, CPC, CPA, CPS, CPL and CPR
Solving linear equations with MATLAB ax=b
Shell basic operator -- arithmetic operator
2022 spring recruitment interview summary
Get screen width and height tool class