当前位置:网站首页>[Niuke] length of the last word of HJ1 string
[Niuke] length of the last word of HJ1 string
2022-06-24 08:58:00 【Uaena_ An】
Three lines of code to do a problem HJ1 The length of the last word in the string
I mean it doesn't include fixed code !
🧸 Reading questions
Output several words , Space off , Output the length of the last word .
🧸 Code
Write the final code directly
#include <iostream>
using namespace std;
int main()
{
string s;
getline(cin,s);
cout<<s.size()-s.rfind(' ')-1<<endl;
return 0;
}
🧸 Decoding code
string s;
establish stringgetline(cin,s);
Receive string consecutively cout<<s.size()-s.rfind(' ')-1<<endl;
use size Subtract the last one ‘ ’( Space ) The location of subtracting 1, because size Is the next position of the last character
So the left open and right closed subtraction is The number of characters .
🧸 Code iteration process
first generation
This is the record from a few months ago
#include <iostream>
using namespace std;
int main()
{
string s;
//cin>>s;//cin Read space or end of newline scanf Empathy
// Method 1 : One character, one character
// char ch = getchar();
// //char ch = cin.get();
// while(ch!='\n')
// {
// s+=ch;
// ch = getchar();
// }
// Mode two :
getline(cin,s);
size_t pos = s.rfind(' ');
if(pos == string::npos)
{
cout <<s.size()<<endl;
}
else{
cout << s.size() - pos-1;
}
return 0;
}
Second generation
Today, a few months later, I did it again
#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;
}
The third generation
When I finish writing , Read the first version of the code , So I think while Circular doing , It's redundant ! With further transformation
#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;
}
The final version
It turned out pos What you do can also be omitted !!!
#include <iostream>
using namespace std;
int main()
{
string s;
getline(cin,s);
cout<<s.size()-s.rfind(' ')-1<<endl;
return 0;
}
Other big guys solve it
Count the last word size
That's direct loop reception , Overwrite the previous words , Direct output size, Bullfrog !
int main() {
string s;
while(cin >> s);
cout << s.size();
return 0;
}
come on. , I wish you get your favorite offer!
边栏推荐
- 【MySQL从入门到精通】【高级篇】(一)字符集的修改与底层原理
- Wan Weiwei, a researcher from Osaka University, Japan, introduced the rapid integration method and application of robot based on WRS system
- 【LeetCode】387. 字符串中的第一个唯一字符
- MBA-day25 最值问题-应用题
- [MySQL from introduction to mastery] [advanced part] (I) character set modification and underlying principle
- Spark - LeftOuterJoin 结果条数与左表条数不一致
- Liunx Mysql安装
- 怎么把mdf和ldf文件导入MySQL workbench中
- 1844. 将所有数字用字符替换
- How to configure environment variables and distinguish environment packaging for multi terminal project of uniapp development
猜你喜欢

阿里资深软件测试工程师推荐测试人员必学——安全测试入门介绍

"Unusual proxy initial value setting is not supported", causes and Solutions

【NOI模拟赛】给国与时光鸡(构造)

OpenCV每日函数 结构分析和形状描述符(7) 寻找多边形(轮廓)/旋转矩形交集

A tip to read on Medium for free

数据中台:数据中台技术架构详解
![[quantitative investment] discrete Fourier transform to calculate array period](/img/0d/aac02463ff403fb1ff871af5ff91fa.png)
[quantitative investment] discrete Fourier transform to calculate array period

Kaformer personal notes

Qingcloud based "real estate integration" cloud solution

110. balanced binary tree recursive method
随机推荐
数据中台:数据采集和抽取的技术栈详解
疫情、失业,2022,我们高喊着摆烂和躺平!
[team management] 25 tips for testing team performance management
216. combined summation III enumeration method
数据中台:国内大厂中台建设架构集锦
Liunx change the port number of vsftpd
It is enough to read this article about ETL. Three minutes will let you understand what ETL is
opencv最大值滤波(不局限于图像)
【牛客】HJ1 字符串最后一个单词的长度
[noi Simulation Competition] geiguo and time chicken (structure)
A tip to read on Medium for free
常用表情符号
【量化投资】离散傅里叶变换求数组周期
Double pointer analog
【LeetCode】541. 反转字符串 II
所说的Get post:请求的区别,你真的知道了吗??????
SLAM14讲中Sophus包的安装问题
Database migration from PostgreSQL to MySQL
双指针模拟
MySQL | 视图《康师傅MySQL从入门到高级》笔记