Skip to content

docs(cpp): 补充循环练习参考答案 - #177

Merged
Charliechen114514 merged 2 commits into
Awesome-Embedded-Learning-Studio:mainfrom
xiaoshuaijie:ch02-2
Aug 30, 2026
Merged

docs(cpp): 补充循环练习参考答案#177
Charliechen114514 merged 2 commits into
Awesome-Embedded-Learning-Studio:mainfrom
xiaoshuaijie:ch02-2

Conversation

@xiaoshuaijie

Copy link
Copy Markdown
Contributor

提交类型

  • 现有文章改进
  • 社区投稿
  • 翻译或双语同步
  • 其他

修改说明

为循环章节补充练习题参考答案,帮助读者从题目要求过渡到完整、可运行的 C++ 实现。

同时修正空心正方形示例输出中的字符间距,使题目描述与参考答案输出保持一致。

具体修改

修改文件:

  • documents/vol1-fundamentals/ch02/02-loops.md

新增以下练习的参考答案:

  1. 打印空心正方形

    • 使用嵌套 for 循环遍历行列
    • 判断当前位置是否为边界
    • 使用 continue 跳过内部空白位置
    • 增加输入合法性检查
  2. 计算阶乘

    • 使用 long long 保存计算结果
    • 使用 for 循环完成累乘
    • 增加正整数输入检查
  3. 查找素数

    • 使用嵌套循环检查因数
    • 找到因数后使用 break 提前退出
    • 输出指定范围内的所有素数
  4. 打印菱形

    • 校验输入必须为正奇数
    • 根据当前行计算星号数量和前导空格
    • 使用统一逻辑生成上半部分和下半部分

影响范围

  • 仅修改教程 Markdown 内容
  • 不涉及站点导航、Frontmatter、CMake 或运行时代码
  • 不影响其他章节和代码示例
  • 参考答案统一使用 C++17 编译命令

验证方式

  • 四个新增代码块均已使用 g++ -std=c++17 -Wall -Wextra -pedantic 编译
  • 四个示例均使用输入 5 运行验证
  • Frontmatter 校验通过:985 个文件全部有效
  • 链接检查通过
  • Markdownlint

已知情况

本地 VitePress 构建在既有英文编译章节失败,原因是缺少:

documents/en/compilation/compilation-linking-2-reuse-concept/static_library.png

该问题与本 PR 修改内容无关。

git diff --check 报告示例输出代码块中的少量行尾空格,这些空格用于保持图案输出的宽度。

@xiaoshuaijie xiaoshuaijie changed the title docs(vol1): 补充循环练习参考答案 docs(cpp): 补充循环练习参考答案 Aug 29, 2026

@Charliechen114514 Charliechen114514 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以看看这些反馈的问题,然后再考虑一下呢?

return 1;
}
std::cout << "2" << "到" << n << "之间所有的素数: " << std::endl;
for (int flag = 0, i = 2; i <= n; i++)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以看到这里,您写的flag实际上有一些冗余,其实最合适的办法,还是请把flag作为一个独立的布尔变量放到外侧(是这样的,以笔者工作为例子,除非是很老的陈旧代码会不规范的使用int作为布尔类型),我当时一看到这里的时候脑子蒙了一下,建议这里优化一下写法哈

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的好的之前习惯了

Comment thread documents/vol1-fundamentals/ch02/02-loops.md
int main()
{
int n = 0;
long long sum = 1;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个地方小小的捉个虫,叫factorial最合适

Comment thread documents/vol1-fundamentals/ch02/02-loops.md

@Charliechen114514 Charliechen114514 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯嗯基本没啥问题了,可以合入

@Charliechen114514
Charliechen114514 merged commit 394c6aa into Awesome-Embedded-Learning-Studio:main Aug 30, 2026
4 checks passed
@xiaoshuaijie
xiaoshuaijie deleted the ch02-2 branch August 30, 2026 11:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants