class="tags" href="/C.html" title=
c>
content_views"
class="tags" href="/C.html" title=
c>
class="markdown_views prism-tomorrow-night">
class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/class="tags" href="/C.html" title=c>cb7f73class="tags" href="/C.html" title=c>cda1a14130840e356class="tags" href="/C.html" title=c>cb0b6f9e4.png#piclass="tags" href="/C.html" title=c>c_class="tags" href="/C.html" title=c>center" alt="在这里插入图片描述" width="100" />
<
class="tags" href="/C.html" title=
c>
center>
博客主页: [小ᶻ☡꙳ᵃⁱᵍᶜ꙳]
class="tags" href="/C.html" title=
c>
center>
<
class="tags" href="/C.html" title=
c>
center>
本文专栏: C++
class="tags" href="/C.html" title=
c>
center>
class="tags" href="/C.html" title=c>c="https://img-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/00d4fdeb7b0d4dbb99e88f325ef249d8.gif#piclass="tags" href="/C.html" title=c>c_class="tags" href="/C.html" title=c>center" alt="在这里插入图片描述" width="1000" height="100" />
💯前言
- 在学习C++编程的过程中class="tags" href="/C.html" title=c>c;字符处理问题是一个重要的实践方向。本文将以NOIP2018普及组的一道题目"标题统计"为切入点class="tags" href="/C.html" title=c>c;详细解析题目要求class="tags" href="/C.html" title=c>c;并对三种实现方法进行对比和优化。文章还会结合题目扩展一些相关知识点class="tags" href="/C.html" title=c>c;帮助读者深入理解字符串操作及其应用。
C++ 参考手册
class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/f0db7class="tags" href="/C.html" title=c>c348331440fa266b3class="tags" href="/C.html" title=c>c24f558694.png#piclass="tags" href="/C.html" title=c>c_class="tags" href="/C.html" title=c>center" alt="在这里插入图片描述" />
💯题目背景
PP5015 [NOIP2018 普及组] 标题统计
class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/aa636223d43b47898a724f01class="tags" href="/C.html" title=c>cclass="tags" href="/C.html" title=c>cclass="tags" href="/C.html" title=c>c2a0ff.png" alt="在这里插入图片描述" />
这道题目源于NOIP2018普及组T1class="tags" href="/C.html" title=c>c;目的是对作文标题中字符的数量进行统计。题目描述如下:
题目描述
凯凯刚写了一篇美妙的作文class="tags" href="/C.html" title=c>c;请问这篇作文的标题中有多少个字符?注意:标题中可能包含大、小写英文字母、数字字符、空格和换行符。统计标题字符数时class="tags" href="/C.html" title=c>c;空格和换行符不计算在内。
输入格式
输入文件只有一行class="tags" href="/C.html" title=c>c;一个字符串 class="tags" href="/C.html" title=c>class="katex--inline">class="tags" href="/C.html" title=c>class="katex">class="tags" href="/C.html" title=c>class="katex-mathml"> s s class="tags" href="/C.html" title=c>class="katex-html">class="tags" href="/C.html" title=c>class="base">class="tags" href="/C.html" title=c>class="strut" style="height: 0.4306em;">class="tags" href="/C.html" title=c>class="mord mathnormal">s。
输出格式
输出文件只有一行class="tags" href="/C.html" title=c>c;包含一个整数class="tags" href="/C.html" title=c>c;即作文标题的字符数(不含空格和换行符)。
样例 #1
样例输入 #1
<class="tags" href="/C.html" title=c>code>234
class="tags" href="/C.html" title=c>code>
样例输出 #1
<class="tags" href="/C.html" title=c>code>3
class="tags" href="/C.html" title=c>code>
样例 #2
样例输入 #2
<class="tags" href="/C.html" title=c>code>Ca 45
class="tags" href="/C.html" title=c>code>
样例输出 #2
<class="tags" href="/C.html" title=c>code>4
class="tags" href="/C.html" title=c>code>
提示
- 样例 1 说明:标题中共有 3 个字符class="tags" href="/C.html" title=c>c;这 3 个字符都是数字字符。
- 样例 2 说明:标题中共有 4 个字符class="tags" href="/C.html" title=c>c;包括 1 个大写字母class="tags" href="/C.html" title=c>c;1 个小写字母和 2 个数字字符(忽略了空格)。
数据规模与约定
- 对于 40% 的数据class="tags" href="/C.html" title=c>c;class="tags" href="/C.html" title=c>class="katex--inline">class="tags" href="/C.html" title=c>class="katex">class="tags" href="/C.html" title=c>class="katex-mathml"> 1 ≤ ∣ s ∣ ≤ 5 1 \leq |s| \leq 5 class="tags" href="/C.html" title=c>class="katex-html">class="tags" href="/C.html" title=c>class="base">class="tags" href="/C.html" title=c>class="strut" style="height: 0.7804em; verticlass="tags" href="/C.html" title=c>cal-align: -0.136em;">class="tags" href="/C.html" title=c>class="mord">1class="tags" href="/C.html" title=c>class="mspaclass="tags" href="/C.html" title=c>ce" style="margin-right: 0.2778em;">class="tags" href="/C.html" title=c>class="mrel">≤class="tags" href="/C.html" title=c>class="mspaclass="tags" href="/C.html" title=c>ce" style="margin-right: 0.2778em;">class="tags" href="/C.html" title=c>class="base">class="tags" href="/C.html" title=c>class="strut" style="height: 1em; verticlass="tags" href="/C.html" title=c>cal-align: -0.25em;">class="tags" href="/C.html" title=c>class="mord">∣class="tags" href="/C.html" title=c>class="mord mathnormal">sclass="tags" href="/C.html" title=c>class="mord">∣class="tags" href="/C.html" title=c>class="mspaclass="tags" href="/C.html" title=c>ce" style="margin-right: 0.2778em;">class="tags" href="/C.html" title=c>class="mrel">≤class="tags" href="/C.html" title=c>class="mspaclass="tags" href="/C.html" title=c>ce" style="margin-right: 0.2778em;">class="tags" href="/C.html" title=c>class="base">class="tags" href="/C.html" title=c>class="strut" style="height: 0.6444em;">class="tags" href="/C.html" title=c>class="mord">5class="tags" href="/C.html" title=c>c;保证输入为数字字符及行末换行符。
- 对于 100% 的数据class="tags" href="/C.html" title=c>c;class="tags" href="/C.html" title=c>class="katex--inline">class="tags" href="/C.html" title=c>class="katex">class="tags" href="/C.html" title=c>class="katex-mathml"> 1 ≤ ∣ s ∣ ≤ 5 1 \leq |s| \leq 5 class="tags" href="/C.html" title=c>class="katex-html">class="tags" href="/C.html" title=c>class="base">class="tags" href="/C.html" title=c>class="strut" style="height: 0.7804em; verticlass="tags" href="/C.html" title=c>cal-align: -0.136em;">class="tags" href="/C.html" title=c>class="mord">1class="tags" href="/C.html" title=c>class="mspaclass="tags" href="/C.html" title=c>ce" style="margin-right: 0.2778em;">class="tags" href="/C.html" title=c>class="mrel">≤class="tags" href="/C.html" title=c>class="mspaclass="tags" href="/C.html" title=c>ce" style="margin-right: 0.2778em;">class="tags" href="/C.html" title=c>class="base">class="tags" href="/C.html" title=c>class="strut" style="height: 1em; verticlass="tags" href="/C.html" title=c>cal-align: -0.25em;">class="tags" href="/C.html" title=c>class="mord">∣class="tags" href="/C.html" title=c>class="mord mathnormal">sclass="tags" href="/C.html" title=c>class="mord">∣class="tags" href="/C.html" title=c>class="mspaclass="tags" href="/C.html" title=c>ce" style="margin-right: 0.2778em;">class="tags" href="/C.html" title=c>class="mrel">≤class="tags" href="/C.html" title=c>class="mspaclass="tags" href="/C.html" title=c>ce" style="margin-right: 0.2778em;">class="tags" href="/C.html" title=c>class="base">class="tags" href="/C.html" title=c>class="strut" style="height: 0.6444em;">class="tags" href="/C.html" title=c>class="mord">5class="tags" href="/C.html" title=c>c;输入可能包含大、小写英文字母、数字字符、空格和行末换行符。
💯方法分析
为了解决这道题目class="tags" href="/C.html" title=c>c;我们探讨了三种主要的实现方式class="tags" href="/C.html" title=c>c;并分析它们的优缺点。
方法1:我的做法
实现代码
<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>cpp">class="tags" href="/C.html" title=c>class="token maclass="tags" href="/C.html" title=c>cro property">class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive-hash">#class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive keyword">inclass="tags" href="/C.html" title=c>clude class="tags" href="/C.html" title=c>class="token string"><iostream>
class="tags" href="/C.html" title=c>class="token maclass="tags" href="/C.html" title=c>cro property">class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive-hash">#class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive keyword">inclass="tags" href="/C.html" title=c>clude class="tags" href="/C.html" title=c>class="token string"><string>
class="tags" href="/C.html" title=c>class="token keyword">using class="tags" href="/C.html" title=c>class="token keyword">namespaclass="tags" href="/C.html" title=c>ce stdclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">int class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">mainclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)
class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>class="token keyword">int class="tags" href="/C.html" title=c>count class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;string sclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">getlineclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>cinclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, sclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 读取整行字符串class="tags" href="/C.html" title=c>class="token keyword">for class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token keyword">int i class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; i class="tags" href="/C.html" title=c>class="token operator">< sclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">sizeclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; iclass="tags" href="/C.html" title=c>class="token operator">++class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>class="token keyword">if class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(sclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">[iclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">] class="tags" href="/C.html" title=c>class="token operator">== class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>char">' 'class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token keyword">class="tags" href="/C.html" title=c>continueclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 跳过空格class="tags" href="/C.html" title=c>countclass="tags" href="/C.html" title=c>class="token operator">++class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}class="tags" href="/C.html" title=c>cout class="tags" href="/C.html" title=c>class="token operator"><< class="tags" href="/C.html" title=c>count class="tags" href="/C.html" title=c>class="token operator"><< endlclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 输出统计结果class="tags" href="/C.html" title=c>class="token keyword">return class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}
class="tags" href="/C.html" title=c>code>
class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/0d0a98170e024863b11663b20464d7a7.png" alt="在这里插入图片描述" />
详细解析
-
输入方式:
- 使用<class="tags" href="/C.html" title=c>code>getline()class="tags" href="/C.html" title=c>code>读取整行输入class="tags" href="/C.html" title=c>c;便于处理包含空格的内容。
-
统计逻辑:
- 遍历字符串中的每个字符。
- 通过判断字符是否为空格(<class="tags" href="/C.html" title=c>code>s[i] == ' 'class="tags" href="/C.html" title=c>code>)class="tags" href="/C.html" title=c>c;跳过统计。
- 对于非空格字符class="tags" href="/C.html" title=c>c;<class="tags" href="/C.html" title=c>code>class="tags" href="/C.html" title=c>countclass="tags" href="/C.html" title=c>code>累加。
-
输出结果:
优点
- 逻辑简单class="tags" href="/C.html" title=c>c;易于理解。
- 手动实现字符过滤逻辑class="tags" href="/C.html" title=c>c;适合初学者练习。
缺点
- 没有考虑换行符和其他空白字符的处理。
- 代码冗长class="tags" href="/C.html" title=c>c;可以通过标准库函数简化。
方法2:老师的第一种做法
实现代码
<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>cpp">class="tags" href="/C.html" title=c>class="token maclass="tags" href="/C.html" title=c>cro property">class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive-hash">#class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive keyword">inclass="tags" href="/C.html" title=c>clude class="tags" href="/C.html" title=c>class="token string"><iostream>
class="tags" href="/C.html" title=c>class="token maclass="tags" href="/C.html" title=c>cro property">class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive-hash">#class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive keyword">inclass="tags" href="/C.html" title=c>clude class="tags" href="/C.html" title=c>class="token string"><string>
class="tags" href="/C.html" title=c>class="token maclass="tags" href="/C.html" title=c>cro property">class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive-hash">#class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive keyword">inclass="tags" href="/C.html" title=c>clude class="tags" href="/C.html" title=c>class="token string"><class="tags" href="/C.html" title=c>cclass="tags" href="/C.html" title=c>ctype>
class="tags" href="/C.html" title=c>class="token keyword">using class="tags" href="/C.html" title=c>class="token keyword">namespaclass="tags" href="/C.html" title=c>ce stdclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">int class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">mainclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)
class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{string sclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">getlineclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>cinclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">, sclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 一次性读取整行输入class="tags" href="/C.html" title=c>class="token keyword">int class="tags" href="/C.html" title=c>cnt class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">for class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token keyword">auto e class="tags" href="/C.html" title=c>class="token operator">: sclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{class="tags" href="/C.html" title=c>class="token keyword">if class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">isspaclass="tags" href="/C.html" title=c>ceclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(eclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 判断是否为空白字符class="tags" href="/C.html" title=c>class="token keyword">class="tags" href="/C.html" title=c>continueclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">elseclass="tags" href="/C.html" title=c>cntclass="tags" href="/C.html" title=c>class="token operator">++class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}class="tags" href="/C.html" title=c>cout class="tags" href="/C.html" title=c>class="token operator"><< class="tags" href="/C.html" title=c>cnt class="tags" href="/C.html" title=c>class="token operator"><< endlclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 输出有效字符数class="tags" href="/C.html" title=c>class="token keyword">return class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}
class="tags" href="/C.html" title=c>code>
class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/ff558class="tags" href="/C.html" title=c>c0982654class="tags" href="/C.html" title=c>c7795954eafa34e0099.png" alt="在这里插入图片描述" />
详细解析
-
输入方式:
- 使用<class="tags" href="/C.html" title=c>code>getline()class="tags" href="/C.html" title=c>code>读取整行输入class="tags" href="/C.html" title=c>c;这种方式能读取包含空格的字符串。
- 不会因为空格而分隔字符串class="tags" href="/C.html" title=c>c;这是统计字符的基础。
-
统计逻辑:
- 遍历字符串中的每个字符。
- 使用<class="tags" href="/C.html" title=c>code>isspaclass="tags" href="/C.html" title=c>ce()class="tags" href="/C.html" title=c>code>判断字符是否为空白字符(包括空格、制表符和换行符)。
- 如果是空白字符class="tags" href="/C.html" title=c>c;则跳过;否则将计数器<class="tags" href="/C.html" title=c>code>class="tags" href="/C.html" title=c>cntclass="tags" href="/C.html" title=c>code>加1。
-
输出结果:
- 统计完成后class="tags" href="/C.html" title=c>c;将有效字符的总数输出。
优点
- 利用了C++标准库中的<class="tags" href="/C.html" title=c>code>isspaclass="tags" href="/C.html" title=c>ce()class="tags" href="/C.html" title=c>code>函数class="tags" href="/C.html" title=c>c;过滤空白字符非常方便。
- 更加通用class="tags" href="/C.html" title=c>c;能处理所有空白字符而非仅空格。
缺点
- 输入内容较大时class="tags" href="/C.html" title=c>c;需一次性加载到内存class="tags" href="/C.html" title=c>c;可能导致性能问题。
方法3:老师的第二种做法
实现代码
<class="tags" href="/C.html" title=c>code class="tags" href="/C.html" title=c>class="prism language-class="tags" href="/C.html" title=c>cpp">class="tags" href="/C.html" title=c>class="token maclass="tags" href="/C.html" title=c>cro property">class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive-hash">#class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive keyword">inclass="tags" href="/C.html" title=c>clude class="tags" href="/C.html" title=c>class="token string"><iostream>
class="tags" href="/C.html" title=c>class="token maclass="tags" href="/C.html" title=c>cro property">class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive-hash">#class="tags" href="/C.html" title=c>class="token direclass="tags" href="/C.html" title=c>ctive keyword">inclass="tags" href="/C.html" title=c>clude class="tags" href="/C.html" title=c>class="token string"><string>
class="tags" href="/C.html" title=c>class="token keyword">using class="tags" href="/C.html" title=c>class="token keyword">namespaclass="tags" href="/C.html" title=c>ce stdclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">int class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">mainclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)
class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{string sclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">int ans class="tags" href="/C.html" title=c>class="token operator">= class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;class="tags" href="/C.html" title=c>class="token keyword">while class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>cin class="tags" href="/C.html" title=c>class="token operator">>> sclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">) class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 按单词读取class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">{ans class="tags" href="/C.html" title=c>class="token operator">+= sclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">.class="tags" href="/C.html" title=c>class="token funclass="tags" href="/C.html" title=c>ction">sizeclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">(class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">)class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 累计单词长度class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}class="tags" href="/C.html" title=c>cout class="tags" href="/C.html" title=c>class="token operator"><< ans class="tags" href="/C.html" title=c>class="token operator"><< endlclass="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">; class="tags" href="/C.html" title=c>class="token class="tags" href="/C.html" title=c>comment">// 输出结果class="tags" href="/C.html" title=c>class="token keyword">return class="tags" href="/C.html" title=c>class="token number">0class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">;
class="tags" href="/C.html" title=c>class="token punclass="tags" href="/C.html" title=c>ctuation">}
class="tags" href="/C.html" title=c>code>
class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/f8713class="tags" href="/C.html" title=c>c1e67794db3b4721class="tags" href="/C.html" title=c>cd318class="tags" href="/C.html" title=c>cd2704.png" alt="在这里插入图片描述" />
详细解析
-
输入方式:
- 使用<class="tags" href="/C.html" title=c>code>class="tags" href="/C.html" title=c>cin >> sclass="tags" href="/C.html" title=c>code>按单词读取输入class="tags" href="/C.html" title=c>c;遇到空格或换行符自动分隔。
- 每次只读取一个单词class="tags" href="/C.html" title=c>c;避免一次性加载所有内容。
-
统计逻辑:
- 对于每次读取的单词class="tags" href="/C.html" title=c>c;直接调用<class="tags" href="/C.html" title=c>code>s.size()class="tags" href="/C.html" title=c>code>获取其长度class="tags" href="/C.html" title=c>c;并累计到<class="tags" href="/C.html" title=c>code>ansclass="tags" href="/C.html" title=c>code>中。
-
输出结果:
- 所有单词处理完成后class="tags" href="/C.html" title=c>c;输出有效字符总数。
优点
- 更加节省内存class="tags" href="/C.html" title=c>c;适合处理大输入数据。
- 逻辑简洁class="tags" href="/C.html" title=c>c;利用<class="tags" href="/C.html" title=c>code>class="tags" href="/C.html" title=c>cinclass="tags" href="/C.html" title=c>code>天然跳过空白字符的特性。
缺点
- 按单词分隔输入class="tags" href="/C.html" title=c>c;可能会在某些特殊场景下不够灵活(如连续空格的处理)。
💯方法对比与优化
方法对比
比较维度 | 方法1:我的做法 | 方法2:老师的第一种做法 | 方法3:老师的第二种做法 |
---|
内存使用 | 较高class="tags" href="/C.html" title=c>c;需要一次性加载整行内容 | 较高class="tags" href="/C.html" title=c>c;需要一次性加载整行内容 | 较低class="tags" href="/C.html" title=c>c;只处理一个单词 |
代码复杂度 | 稍高class="tags" href="/C.html" title=c>c;需要手动判断字符 | 较低class="tags" href="/C.html" title=c>c;使用标准库函数<class="tags" href="/C.html" title=c>code>isspaclass="tags" href="/C.html" title=c>ce()class="tags" href="/C.html" title=c>code> | 最低class="tags" href="/C.html" title=c>c;直接利用<class="tags" href="/C.html" title=c>code>class="tags" href="/C.html" title=c>cinclass="tags" href="/C.html" title=c>code>分隔单词 |
适用场景 | 输入内容较小或初学者练习 | 输入内容较小且需要逐字符处理的场景 | 输入内容较大或逐单词处理的场景 |
优化建议
-
统一有效字符的定义:
- 如果题目要求更加严格class="tags" href="/C.html" title=c>c;可以结合<class="tags" href="/C.html" title=c>code>isalnum()class="tags" href="/C.html" title=c>code>函数class="tags" href="/C.html" title=c>c;确保只统计字母和数字字符。
-
增强鲁棒性:
- 在读取和统计时添加更多异常处理逻辑class="tags" href="/C.html" title=c>c;如过滤特殊符号。
-
结合方法:
- 将方法1和方法2结合class="tags" href="/C.html" title=c>c;按实际需求选择读取方式class="tags" href="/C.html" title=c>c;优化性能和灵活性。
💯扩展与思考
1. 字符串处理常用函数
- <class="tags" href="/C.html" title=c>code>isspaclass="tags" href="/C.html" title=c>ce(class="tags" href="/C.html" title=c>char class="tags" href="/C.html" title=c>c)class="tags" href="/C.html" title=c>code>:判断字符是否为空白字符。
- <class="tags" href="/C.html" title=c>code>isalnum(class="tags" href="/C.html" title=c>char class="tags" href="/C.html" title=c>c)class="tags" href="/C.html" title=c>code>:判断字符是否为字母或数字。
- <class="tags" href="/C.html" title=c>code>tolower(class="tags" href="/C.html" title=c>char class="tags" href="/C.html" title=c>c)class="tags" href="/C.html" title=c>code> / <class="tags" href="/C.html" title=c>code>toupper(class="tags" href="/C.html" title=c>char class="tags" href="/C.html" title=c>c)class="tags" href="/C.html" title=c>code>:将字符转换为小写或大写。
2. 输入方式的选择
- <class="tags" href="/C.html" title=c>code>getline()class="tags" href="/C.html" title=c>code>:
- 适合处理整行输入class="tags" href="/C.html" title=c>c;尤其是包含空格的内容。
- <class="tags" href="/C.html" title=c>code>class="tags" href="/C.html" title=c>cin >> sclass="tags" href="/C.html" title=c>code>:
- 适合按单词分隔的场景class="tags" href="/C.html" title=c>c;自带空白字符过滤。
3. 实际应用场景
- 文本分析:如统计单词数、字符频率。
- 数据过滤:如提取特定格式的内容。
💯小结
通过对NOIP2018普及组"标题统计"题目的深入分析class="tags" href="/C.html" title=c>c;我们探讨了三种主要的实现方法class="tags" href="/C.html" title=c>c;并对其优缺点和适用场景进行了详细对比。无论是整行读取还是按单词读取class="tags" href="/C.html" title=c>c;都有各自的优势class="tags" href="/C.html" title=c>c;可以根据具体需求灵活选择。在实际编程中class="tags" href="/C.html" title=c>c;合理选择输入方式和统计逻辑class="tags" href="/C.html" title=c>c;不仅能提高代码的效率class="tags" href="/C.html" title=c>c;还能增强其鲁棒性。
class="tags" href="/C.html" title=c>c="https://img-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/00d4fdeb7b0d4dbb99e88f325ef249d8.gif#piclass="tags" href="/C.html" title=c>c_class="tags" href="/C.html" title=c>center" alt="在这里插入图片描述" width="1000" height="100" />
class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />class="tags" href="/C.html" title=c>c="https://i-blog.class="tags" href="/C.html" title=c>csdnimg.class="tags" href="/C.html" title=c>cn/direclass="tags" href="/C.html" title=c>ct/fclass="tags" href="/C.html" title=c>c6aclass="tags" href="/C.html" title=c>c6d93b4745b591bb8a8e48f86779.png#piclass="tags" href="/C.html" title=c>c_right" alt="在这里插入图片描述" width="1" />