【C++】PP5015 [NOIP2018 普及组] 标题统计

devtools/2025/1/16 13:24:52/
class="tags" href="/C.html" title=c>cle class="tags" href="/C.html" title=c>class="baidu_pl">
class="tags" href="/C.html" title=c>cle_class="tags" href="/C.html" title=c>content" class="tags" href="/C.html" title=c>class="articlass="tags" href="/C.html" title=c>cle_class="tags" href="/C.html" title=c>content class="tags" href="/C.html" title=c>clearfix">
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>cap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-bloclass="tags" href="/C.html" title=c>ck" style="-webkit-tap-highlight-class="tags" href="/C.html" title=c>color: rgba(0, 0, 0, 0);">

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>class="toclass="tags" href="/C.html" title=c>c">

文章目录

  • 💯前言
  • 💯题目背景
    • 题目描述
    • 输入格式
    • 输出格式
    • 样例 #1
      • 样例输入 #1
      • 样例输出 #1
    • 样例 #2
      • 样例输入 #2
      • 样例输出 #2
    • 提示
    • 数据规模与约定
  • 💯方法分析
    • 方法1:我的做法
      • 实现代码
      • 详细解析
      • 优点
      • 缺点
    • 方法2:老师的第一种做法
      • 实现代码
      • 详细解析
      • 优点
      • 缺点
    • 方法3:老师的第二种做法
      • 实现代码
      • 详细解析
      • 优点
      • 缺点
  • 💯方法对比与优化
    • 方法对比
    • 优化建议
  • 💯扩展与思考
    • 1. 字符串处理常用函数
    • 2. 输入方式的选择
    • 3. 实际应用场景
  • 💯小结


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普及组T1࿰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>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="在这里插入图片描述" />

详细解析

  1. 输入方式:

    • 使用<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;便于处理包含空格的内容。
  2. 统计逻辑:

    • 遍历字符串中的每个字符。
    • 通过判断字符是否为空格(<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>累加。
  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;可以通过标准库函数简化。

方法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="在这里插入图片描述" />

详细解析

  1. 输入方式:

    • 使用<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;这是统计字符的基础。
  2. 统计逻辑:

    • 遍历字符串中的每个字符。
    • 使用<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。
  3. 输出结果:

    • 统计完成后࿰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="在这里插入图片描述" />

详细解析

  1. 输入方式:

    • 使用<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;避免一次性加载所有内容。
  2. 统计逻辑:

    • 对于每次读取的单词࿰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>中。
  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>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>分隔单词
适用场景输入内容较小或初学者练习输入内容较小且需要逐字符处理的场景输入内容较大或逐单词处理的场景

优化建议

  1. 统一有效字符的定义:

    • 如果题目要求更加严格࿰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;确保只统计字母和数字字符。
  2. 增强鲁棒性:

    • 在读取和统计时添加更多异常处理逻辑࿰class="tags" href="/C.html" title=c>c;如过滤特殊符号。
  3. 结合方法:

    • 将方法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" />

class="tags" href="/C.html" title=c>class="blog-vote-box">

http://www.ppmy.cn/devtools/150962.html

相关文章

Type-C双屏显示器方案

在数字化时代&#xff0c;高效的信息处理和视觉体验已成为我们日常生活和工作的关键需求。随着科技的进步&#xff0c;一款结合了便携性和高效视觉输出的设备——双屏便携屏&#xff0c;逐渐崭露头角&#xff0c;成为追求高效工作和娱乐体验人群的新宠。本文将深入探讨双屏便携…

Windows 正确配置android adb调试的方法

下载适用于 Windows 的 SDK Platform-Tools https://developer.android.google.cn/tools/releases/platform-tools?hlzh-cn 设置系统变量&#xff0c;路径为platform-tools文件夹的绝对路径 点击Path添加环境变量 %adb%打开终端输入adb shell 这就成功了&#xff01;

React 中事件机制详细介绍:概念与执行流程如何更好的理解

React 的事件机制是一个非常重要的概念&#xff0c;它涉及到 React 如何处理用户的交互事件。React 的事件系统与传统的 DOM 事件系统有所不同&#xff0c;它在底层使用了事件委托和合成事件&#xff08;Synthetic Events&#xff09;来优化性能。下面&#xff0c;我们将从 Rea…

3_CSS3 渐变 --[CSS3 进阶之路]

CSS3 引入了渐变&#xff08;gradients&#xff09;&#xff0c;它允许在两个或多个指定的颜色之间显示平滑的过渡。CSS3 支持两种类型的渐变&#xff1a; 线性渐变&#xff08;Linear Gradients&#xff09;&#xff1a;颜色沿着一条线性路径变化&#xff0c;可以是水平、垂直…

代码随想录算法训练营第三十四天-动态规划-62. 不同路径

动规五部曲 dp数组含义&#xff1a;到达到第i行&#xff0c;第j列的所有不同种走法&#xff0c;即有多少条路径可到达递推公式&#xff1a;dp[i][j] dp[i - 1][j] dp[i][j - 1]dp数组初始化&#xff0c;要初始化最上面一行&#xff0c;与最左边一列的值&#xff0c;都为1&…

女性机器人有市场吗

随着AI技术和仿生技术的发展&#xff0c;可以预见&#xff0c;未来的市场上必然出现女性机器人&#xff0c;女性机器人未来会有市场吗&#xff1f;如何定义女性机器人&#xff1f; 1、如果你不想生娃&#xff0c;女性机器人完全可以代替真人。将来的机器人她能干几乎所有的家务…

WPS计算机二级•表格函数计算

听说这里是目录哦 函数基础知识 相对绝对混合引用&#x1f32a;️相对引用绝对引用混合引用 常用求和函数 SUM函数&#x1f326;️语法说明 函数快速求 平均数最值⚡平均数最值 实用统计函数 实现高效统计&#x1f300;COUNTCOUNTIF 实用文本函数 高效整理数据&#x1f308;RIG…

Go语言的数据竞争 (Data Race) 和 竞态条件 (Race Condition)

文章精选推荐 1 JetBrains Ai assistant 编程工具让你的工作效率翻倍 2 Extra Icons&#xff1a;JetBrains IDE的图标增强神器 3 IDEA插件推荐-SequenceDiagram&#xff0c;自动生成时序图 4 BashSupport Pro 这个ides插件主要是用来干嘛的 &#xff1f; 5 IDEA必装的插件&…