当前位置:网站首页>【牛客】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!
边栏推荐
- 1704. 判断字符串的两半是否相似
- 2022 spring recruitment interview summary
- Why do you want to file? What are the advantages and disadvantages of website filing?
- rsync做文件备份
- Qt 中发送自定义事件
- 数据库迁移从PostgreSQL迁移到 MYSQL
- 打印出来的对象是[object object],解决方法
- 2020中国全国各省市,三级联动数据,数据机构(数据来自国家统计局官网)
- Jenkins自动化部署,连接不到所依赖的服务【已解决】
- What is the future development trend of Business Intelligence BI
猜你喜欢

Mysql数据(Liunx环境)定时备份

Matlab camera calibrator camera calibration

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

Why can ping fail while traceroute can

What is SRE? A detailed explanation of SRE operation and maintenance system

Database migration from PostgreSQL to MySQL

【团队管理】测试团队绩效管理的25点小建议

pymysql 向MySQL 插入数据无故报错

数据库迁移从PostgreSQL迁移到 MYSQL

Jenkins自动化部署,连接不到所依赖的服务【已解决】
随机推荐
One development skill a day: how to establish P2P communication based on webrtc?
2022.06.23(LC_144,94,145_二叉树的前序、中序、后序遍历)
ZUCC_ Principles of compiling language and compilation_ Experiment 0607 grammar analysis ll analysis
The form image uploaded in chorme cannot view the binary image information of the request body
leetcode 1268. Search suggestions system
Battle history between redis and me under billion level traffic
It is enough to read this article about ETL. Three minutes will let you understand what ETL is
深度学习与神经网络:最值得关注的6大趋势
2022春招面试总结
The pie chart with dimension lines can set various parameter options
定时备份数据库脚本
Mysql数据(Liunx环境)定时备份
【NOI模拟赛】给国与时光鸡(构造)
关于ETL看这篇文章就够了,三分钟让你明白什么是ETL
Jenkins自动化部署,连接不到所依赖的服务【已解决】
工具类
[force deduction 10 days SQL introduction] Day3
疫情、失业,2022,我们高喊着摆烂和躺平!
PHP代码加密+扩展解密实战
微博撰写-流程图-序列图-甘特图-mermaid流程图-效果不错