Nested loops and patterns · 嵌套循环与图案
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
A loop inside a loop
- You can put one loop inside another. The inner loop runs fully for each step of the outer loop.
- This is how you work with rows and columns, or compare every pair of items.
- If the outer loop runs
Rtimes and the inner runsCtimes, the inner body runsR × Ctimes.
中文
循环里套循环
- 你可以把一个循环放进另一个循环里面。外层循环每走一步,内层循环都会完整地跑一遍。
- 这就是处理行和列、或者比较每一对元素的方法。
- 如果外层循环跑
R次,内层跑C次,那么内层的代码体一共跑R × C次。
English
Rows and columns
- Think of the outer loop as the row and the inner loop as the column.
- For a block of
*, the outer loop picks a row; the inner loop prints the stars in that row. - After the inner loop finishes a row, print a newline
\nto start the next row.
中文
行与列
- 把外层循环想成行,内层循环想成列。
- 对于一块
*,外层循环选一行;内层循环打印那一行里的星号。 - 内层循环跑完一行后,打印一个换行
\n,开始下一行。
English
Building a line, then a block
printf("*")prints one star without moving to a new line.- Print all the stars for one row first, then one
printf("\n")to end the row. - Repeat for every row, and the rows stack into a block.
中文
先拼一行,再拼一块
printf("*")打印一个星号,但不换行。- 先打印完一行的所有星号,再用一个
printf("\n")结束这一行。 - 每一行都这样做,行就叠成了一块。
#include <stdio.h>
int main(void) {
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
printf("*");
}
printf("\n"); // end the row
}
return 0;
}
English
Counting with nested loops
- Nested loops also count pairs. To count pairs
(i, j)withi < j, start the inner loop ati + 1. - A counter inside the inner loop adds up the matches.
- Watch the start and end values — off-by-one mistakes are common here.
中文
用嵌套循环来计数
- 嵌套循环也能数“对”。要数满足
i < j的对(i, j),就让内层循环从i + 1开始。 - 在内层循环里放一个计数器,把满足条件的对加起来。
- 注意起始值和结束值 —— 这里很容易出现差一(off-by-one)错误。
English
Common mistakes
- The inner loop runs fully for each single turn of the outer loop.
- Check the bounds so a pattern has no extra or missing row.
中文
常见错误
- 外层每转一次,内层要跑完全部。
- 检查边界,别让图案多一行或少一行。
English
Now you try
- Use an inner
printfwithout\nto build a row, then aprintf("\n")to end it. - For the function task, do not write a
main— the checker provides one.
中文
现在轮到你了
- 用不带
\n的内层printf拼出一行,再用printf("\n")结束这一行。 - 函数任务里,不要自己写
main—— 检查器会提供。
In main, use nested loops to print a 3×3 block of * — three stars on each of three lines. · 在 main 里面,用嵌套循环打印一个 3×3 的 * 方块 —— 三行,每行三个星号。
Click Run to see the output here. · 点击“运行”查看此处输出。
In main, print a left-aligned triangle of * that is 4 rows tall: row 1 has one *, row 2 has two, up to row 4 with four. · 在 main 里面,打印一个靠左对齐、共 4 行高的 * 三角形:第 1 行一个 *,第 2 行两个,一直到第 4 行四个。
Click Run to see the output here. · 点击“运行”查看此处输出。
Complete int count_pairs(int n) so it returns how many pairs (i, j) satisfy 1 <= i < j <= n. Use nested loops (start the inner loop at i + 1). Do not · 不 write a main. · 完成 int count_pairs(int n),让它返回满足 1 <= i < j <= n 的对 (i, j) 有多少个。用嵌套循环(内层循环从 i + 1 开始)。不要写 main。
Click Run to see the output here. · 点击“运行”查看此处输出。