当前位置:网站首页>[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!
边栏推荐
- Spark - LeftOuterJoin 结果条数与左表条数不一致
- 4275. Dijkstra sequence
- 数据中台:民生银行的数据中台实践方案
- Deep learning and neural networks: the six most noteworthy trends
- 第七章 操作位和位串(三)
- [pytoch basic tutorial 31] youtubednn model analysis
- 数据中台:中台实践与总结
- Liunx change the port number of vsftpd
- Prompt code when MySQL inserts Chinese data due to character set problems: 1366
- Data middle office: the data middle office practice scheme of Minsheng Bank
猜你喜欢

Become an IEEE student member

One article explains in detail | those things about growth

什么是图神经网络?图神经网络有什么用?

MySQL | 视图《康师傅MySQL从入门到高级》笔记
![[quantitative investment] discrete Fourier transform to calculate array period](/img/0d/aac02463ff403fb1ff871af5ff91fa.png)
[quantitative investment] discrete Fourier transform to calculate array period
![[MySQL from introduction to mastery] [advanced part] (I) character set modification and underlying principle](/img/db/e581087e550a2e460f12047685c48f.png)
[MySQL from introduction to mastery] [advanced part] (I) character set modification and underlying principle

K8s deployment of highly available PostgreSQL Cluster -- the road to building a dream

A tip to read on Medium for free
![Jenkins is deployed automatically and cannot connect to the dependent service [solved]](/img/fe/f294955a9bdf7492aab360e44e052d.png)
Jenkins is deployed automatically and cannot connect to the dependent service [solved]

MySQL | store notes of Master Kong MySQL from introduction to advanced
随机推荐
阿里资深软件测试工程师推荐测试人员必学——安全测试入门介绍
Spark - LeftOuterJoin 结果条数与左表条数不一致
等保备案是什么意思?应该去哪里办理备案?
数据中台:数据采集和抽取的技术栈详解
Data midrange: analysis of full stack technical architecture of data midrange, with industry solutions
打印出来的对象是[object object],解决方法
K8s deployment of highly available PostgreSQL Cluster -- the road to building a dream
threejs辉光通道01(UnrealBloomPass && layers)
520. detect capital letters
input的聚焦后的边框问题
玄铁E906移植----番外0:玄铁C906仿真环境搭建
WebRTC系列-网络传输之5选择最优connection切换
微博撰写-流程图-序列图-甘特图-mermaid流程图-效果不错
疫情、失业,2022,我们高喊着摆烂和躺平!
Telnet port login method with user name for liunx server
2138. splitting a string into groups of length k
Leetcode -- wrong set
基于QingCloud的 “房地一体” 云解决方案
MBA-day25 最值问题-应用题
216. 组合总和 III-枚举法