docs(cpp): 补充循环练习参考答案 - #177
Merged
Charliechen114514 merged 2 commits intoAug 30, 2026
Merged
Conversation
Charliechen114514
requested changes
Aug 30, 2026
Charliechen114514
left a comment
Member
There was a problem hiding this comment.
可以看看这些反馈的问题,然后再考虑一下呢?
| return 1; | ||
| } | ||
| std::cout << "2" << "到" << n << "之间所有的素数: " << std::endl; | ||
| for (int flag = 0, i = 2; i <= n; i++) |
Member
There was a problem hiding this comment.
可以看到这里,您写的flag实际上有一些冗余,其实最合适的办法,还是请把flag作为一个独立的布尔变量放到外侧(是这样的,以笔者工作为例子,除非是很老的陈旧代码会不规范的使用int作为布尔类型),我当时一看到这里的时候脑子蒙了一下,建议这里优化一下写法哈
| int main() | ||
| { | ||
| int n = 0; | ||
| long long sum = 1; |
Member
There was a problem hiding this comment.
这个地方小小的捉个虫,叫factorial最合适
Charliechen114514
merged commit Aug 30, 2026
394c6aa
into
Awesome-Embedded-Learning-Studio:main
4 checks passed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
提交类型
修改说明
为循环章节补充练习题参考答案,帮助读者从题目要求过渡到完整、可运行的 C++ 实现。
同时修正空心正方形示例输出中的字符间距,使题目描述与参考答案输出保持一致。
具体修改
修改文件:
documents/vol1-fundamentals/ch02/02-loops.md新增以下练习的参考答案:
打印空心正方形
for循环遍历行列continue跳过内部空白位置计算阶乘
long long保存计算结果for循环完成累乘查找素数
break提前退出打印菱形
影响范围
验证方式
g++ -std=c++17 -Wall -Wextra -pedantic编译5运行验证已知情况
本地 VitePress 构建在既有英文编译章节失败,原因是缺少:
documents/en/compilation/compilation-linking-2-reuse-concept/static_library.png该问题与本 PR 修改内容无关。
git diff --check报告示例输出代码块中的少量行尾空格,这些空格用于保持图案输出的宽度。