当前位置:网站首页>[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!
边栏推荐
- 110. 平衡二叉树-递归法
- What is graph neural network? Figure what is the use of neural networks?
- SLAM14讲中Sophus包的安装问题
- "I can't understand Sudoku, so I choose to play Sudoku."
- 随笔-反思
- Transplantation of xuantie e906 -- fanwai 0: Construction of xuantie c906 simulation environment
- linux(centos7.9)安装部署mysql-cluster 7.6
- 2021-05-20computed和watch应用与区别
- 常用表情符号
- 1528. rearrange strings
猜你喜欢

À propos de ETL il suffit de lire cet article, trois minutes pour vous faire comprendre ce qu'est ETL

Liunx Mysql安装

110. 平衡二叉树-递归法

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

What is the future development trend of Business Intelligence BI

陆奇:我现在最看好这四大技术趋势

4274. 后缀表达式

MySQL | 存储《康师傅MySQL从入门到高级》笔记

Data midrange: detailed explanation of the technical stack of data acquisition and extraction
![打印出来的对象是[object object],解决方法](/img/fc/9199e26b827a1c6304fcd250f2301e.png)
打印出来的对象是[object object],解决方法
随机推荐
Data middle office: a collection of middle office construction architectures of large domestic factories
SLAM14讲中Sophus包的安装问题
什么是图神经网络?图神经网络有什么用?
threejs辉光通道01(UnrealBloomPass && layers)
uniapp 开发多端项目如何配置环境变量以及区分环境打包
Scheme of alcohol concentration tester based on single chip microcomputer
4274. 后缀表达式
[pytoch basic tutorial 31] youtubednn model analysis
Wan Weiwei, a researcher from Osaka University, Japan, introduced the rapid integration method and application of robot based on WRS system
WebRTC系列-网络传输之5选择最优connection切换
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
Spark - LeftOuterJoin 结果条数与左表条数不一致
【NOI模拟赛】给国与时光鸡(构造)
On the routing tree of gin
2022.06.23(LC_144,94,145_二叉树的前序、中序、后序遍历)
Fast and slow pointer series
It is enough to read this article about ETL. Three minutes will let you understand what ETL is
Why can ping fail while traceroute can
“论解不了数独所以选择做个数独游戏这件事”
阿里资深软件测试工程师推荐测试人员必学——安全测试入门介绍