此页面需较新浏览器(支持 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.
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.
English
Building a line, then a block
printf("*")orprintf("%4d", n)prints one item without moving to a new line.- Print all the items for one row first, then one
printf("\n")to end the row. - Repeat for every row, and the rows stack into a block.
English
#include <stdio.h>
int main(void) {
for (int row = 1; row <= 3; row++) {
for (int col = 1; col <= 4; col++) {
printf("%4d", row * col); // 4 characters wide, so columns line up
}
printf("\n"); // end the row
}
return 0;
}
1 2 3 4
2 4 6 8
3 6 9 12
row |
col runs |
Printed in this row |
|---|---|---|
| 1 | 1, 2, 3, 4 | 1 2 3 4, then \n |
| 2 | 1, 2, 3, 4 | 2 4 6 8, then \n |
| 3 | 1, 2, 3, 4 | 3 6 9 12, then \n |
The inner body runs 3 × 4 = 12 times.
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.
English
#include <stdio.h>
int main(void) {
char team[] = {'A', 'B', 'C'};
int games = 0;
for (int i = 0; i < 3; i++) {
for (int j = i + 1; j < 3; j++) { // j starts after i: no A-A, and no B-A after A-B
printf("%c plays %c\n", team[i], team[j]);
games++;
}
}
printf("%d games\n", games);
return 0;
}
A plays B
A plays C
B plays C
3 games
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.
printf("\n")inside the inner loop puts every star on its own line. It belongs after the inner loop.- Using the same variable for both loops (
iinsidei) breaks the outer loop. Give each loop its own variable. - A triangle needs an inner bound that changes with the row, such as
col < row, not a fixed number.
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.
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
In main, use nested loops to print a 3×3 block of * — three stars on each of three lines.
点击“运行”查看此处输出。
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
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.
点击“运行”查看此处输出。
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
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 不 write a main.
点击“运行”查看此处输出。