C 语言程序设计基础练习:循环、分支、数组与矩阵

Eviarch Lv2

本文整理两组 C 语言程序设计练习,共 9 道题,覆盖循环、分支、一维数组、二维数组、排序和矩阵操作。

和单纯堆放答案相比,本文更关注三件事:

  • 题目真正考查什么;
  • 代码为什么这样写;
  • 哪些边界和常见错误值得注意。

以下代码按 C11 编写,二维数组统一预设最大尺寸为 100。若题目数据范围不同,请同步调整 MAX_SIZE。

第一部分:循环与分支

1. 求序列平方和

题目

输入一个整数 n,计算:

1
1 × 1 + 2 × 2 + 3 × 3 + ... + n × n

示例输入:

1
5

示例输出:

1
55

思路

从 1 循环到 n,把每一项的平方累加即可。结果增长很快,因此使用 long long,避免 int 溢出。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>

int main(void) {
int n;

if (scanf("%d", &n) != 1 || n < 0) {
return 1;
}

long long sum = 0;
for (int i = 1; i <= n; ++i) {
sum += (long long)i * i;
}

printf("%lld\n", sum);
return 0;
}

复杂度与易错点

  • 时间复杂度:O(n)
  • 空间复杂度:O(1)
  • 易错点:直接用 int 保存结果,在 n 较大时可能溢出。
  • 如果题目允许极大输入,可以直接使用公式 n(n+1)(2n+1)/6,但仍要注意中间乘法溢出。

2. 水仙花数

题目

输入一个整数 n,其中 100 <= n <= 999,输出 100 到 n 之间的所有水仙花数。水仙花数是指各位数字的立方和等于该数本身的三位数。

示例输入:

1
562

示例输出:

1
2
3
4
153
370
371
407

思路

依次枚举三位数,拆分出百位、十位和个位,再用整数乘法计算立方和。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>

int main(void) {
int n;

if (scanf("%d", &n) != 1 || n < 100 || n > 999) {
return 1;
}

for (int value = 100; value <= n; ++value) {
int hundreds = value / 100;
int tens = value / 10 % 10;
int units = value % 10;

int sum = hundreds * hundreds * hundreds
+ tens * tens * tens
+ units * units * units;

if (sum == value) {
printf("%d\n", value);
}
}

return 0;
}

复杂度与易错点

  • 时间复杂度:O(n)
  • 空间复杂度:O(1)
  • 不建议使用 pow(x, 3) 判断三次方。pow 返回浮点数,参与整数比较时可能产生不必要的精度问题。

3. 求某年某月的天数

题目

输入年份和月份,输出该月天数;如果月份不在 1 到 12 之间,则输出 Error。

示例输入:

1
2
2024
2

示例输出:

1
29

思路

先用数组保存每个月的常规天数,再单独处理闰年二月。

闰年规则:

  • 能被 4 整除但不能被 100 整除;或者
  • 能被 400 整除。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>

int main(void) {
int year;
int month;

if (scanf("%d %d", &year, &month) != 2) {
return 1;
}

if (month < 1 || month > 12) {
printf("Error\n");
return 0;
}

static const int days[] = {
0, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31
};

int result = days[month];

int is_leap_year =
(year % 4 == 0 && year % 100 != 0) || year % 400 == 0;

if (month == 2 && is_leap_year) {
result = 29;
}

printf("%d\n", result);
return 0;
}

复杂度与易错点

  • 时间复杂度:O(1)
  • 空间复杂度:O(1)
  • 闰年判断必须同时考虑“能被 100 整除”和“能被 400 整除”的特殊情况。

4. 最大公约数与最小公倍数

题目

输入两个正整数,输出它们的最大公约数和最小公倍数,用空格分隔。

示例输入:

1
44 36

示例输出:

1
4 396

思路

使用欧几里得算法计算最大公约数:

1
gcd(a, b) = gcd(b, a % b)

得到最大公约数后,最小公倍数可以写成:

1
lcm(a, b) = a / gcd(a, b) * b

先除后乘可以减小中间结果,但最终结果仍可能超出 long long 范围。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>

long long gcd(long long a, long long b) {
while (b != 0) {
long long remainder = a % b;
a = b;
b = remainder;
}
return a;
}

int main(void) {
long long a;
long long b;

if (scanf("%lld %lld", &a, &b) != 2 || a <= 0 || b <= 0) {
return 1;
}

long long greatest = gcd(a, b);
long long least = a / greatest * b;

printf("%lld %lld\n", greatest, least);
return 0;
}

复杂度与易错点

  • 时间复杂度:O(log(min(a, b)))
  • 空间复杂度:O(1)
  • 逐个递减求公约数或逐个递增求公倍数虽然直观,但效率较低。
  • 使用 a * b / gcd(a, b) 时,a * b 可能先溢出。

第二部分:数组、排序与矩阵

5. 矩阵顺时针旋转

题目

输入矩阵的行数和列数,再输入矩阵,输出矩阵顺时针旋转 90 度后的结果。

示例输入:

1
2
3
2 3
1 2 3
4 5 6

示例输出:

1
2
3
4 1
5 2
6 3

思路

顺时针旋转后,新矩阵的第 j 列来自原矩阵第 j 列,但行顺序需要反过来。

对于原矩阵元素 matrix[i][j],旋转后的位置是:

1
rotated[j][rows - 1 - i]

如果只需要输出,也可以按“从左到右遍历原矩阵的列,从下到上遍历每一行”直接打印。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>

#define MAX_SIZE 100

int main(void) {
int rows;
int cols;

if (scanf("%d %d", &rows, &cols) != 2 ||
rows < 1 || rows > MAX_SIZE ||
cols < 1 || cols > MAX_SIZE) {
return 1;
}

int matrix[MAX_SIZE][MAX_SIZE];

for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (scanf("%d", &matrix[i][j]) != 1) {
return 1;
}
}
}

for (int j = 0; j < cols; ++j) {
for (int i = rows - 1; i >= 0; --i) {
if (i != rows - 1) {
putchar(' ');
}
printf("%d", matrix[i][j]);
}
putchar('\n');
}

return 0;
}

复杂度与易错点

  • 时间复杂度:O(rows × cols)
  • 空间复杂度:O(rows × cols)
  • 旋转后矩阵的行数是原列数,列数是原行数。
  • 输出时注意行末不要留下多余空格。

6. 两个数组的并集

题目

输入两个乱序数组,输出去重后按降序排列的并集。

原题样例中第二组数组的长度写成了 2,但实际给出了 4 个元素。下面将样例修正为 4,避免产生歧义。

示例输入:

1
2
3
4
4
1 2 3 4
4
3 5 1 2

示例输出:

1
5 4 3 2 1

思路

把两个数组的元素合并到一个临时数组中,按降序排序,再跳过相邻的重复元素输出。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

#define MAX_TOTAL 200

int compare_desc(const void *left, const void *right) {
int a = *(const int *)left;
int b = *(const int *)right;

if (a < b) {
return 1;
}
if (a > b) {
return -1;
}
return 0;
}

int main(void) {
int first_count;
int second_count;
int values[MAX_TOTAL];
int total = 0;

if (scanf("%d", &first_count) != 1 ||
first_count < 0 || first_count > MAX_TOTAL) {
return 1;
}

for (int i = 0; i < first_count; ++i) {
if (scanf("%d", &values[total]) != 1) {
return 1;
}
++total;
}

if (scanf("%d", &second_count) != 1 ||
second_count < 0 || second_count > MAX_TOTAL - total) {
return 1;
}

for (int i = 0; i < second_count; ++i) {
if (scanf("%d", &values[total]) != 1) {
return 1;
}
++total;
}

qsort(values, total, sizeof(values[0]), compare_desc);

bool first = true;
for (int i = 0; i < total; ++i) {
if (i > 0 && values[i] == values[i - 1]) {
continue;
}

if (!first) {
putchar(' ');
}

printf("%d", values[i]);
first = false;
}

putchar('\n');
return 0;
}

复杂度与易错点

  • 时间复杂度:O((a + b) log(a + b))
  • 空间复杂度:O(a + b)
  • 去重时只需要跳过排序后的相邻重复值。
  • 题目中的“排序好”如果没有明确方向,应结合样例判断升降序;本题样例要求降序。

7. 矩阵乘法

题目

输入一个整数 n,再输入两个 n × n 方阵,输出它们的乘积。每个输出元素占 10 个字符宽度并右对齐。

示例输入:

1
2
3
4
5
2
1 2
3 4
1 2
1 1

示例输出:

1
2
3         4
7 10

思路

矩阵乘法的核心公式是:

1
C[i][j] = Σ(A[i][k] × B[k][j])

最朴素的三重循环时间复杂度为 O(n^3)。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>

#define MAX_SIZE 100

int main(void) {
int n;

if (scanf("%d", &n) != 1 || n < 1 || n > MAX_SIZE) {
return 1;
}

long long first[MAX_SIZE][MAX_SIZE] = {0};
long long second[MAX_SIZE][MAX_SIZE] = {0};
long long result[MAX_SIZE][MAX_SIZE] = {0};

for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (scanf("%lld", &first[i][j]) != 1) {
return 1;
}
}
}

for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (scanf("%lld", &second[i][j]) != 1) {
return 1;
}
}
}

for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < n; ++k) {
result[i][j] += first[i][k] * second[k][j];
}
}
}

for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
printf("%10lld", result[i][j]);
}
putchar('\n');
}

return 0;
}

复杂度与易错点

  • 时间复杂度:O(n^3)
  • 空间复杂度:O(n^2)
  • 矩阵乘法不满足交换律:A × B 通常不等于 B × A。
  • 结果矩阵在累加前必须初始化为 0。
  • 元素乘积与累加结果可能超出 int,建议使用 long long。

8. 环形方阵

题目

输入一个正整数 n,按示例规律输出一个边长为 2n - 1 的方阵。

以 n = 3 为例:

1
2
3
4
5
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1

思路

这个题常被称作“魔方阵”,但它并不是数学意义上的幻方,而是从外向内递增的同心方阵。

对位置 (i, j),它距离上、下、左、右四条边界的距离分别是:

1
2
3
4
i
j
size - 1 - i
size - 1 - j

取四者的最小值再加 1,就是该位置应输出的数字。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>

int main(void) {
int n;

if (scanf("%d", &n) != 1 || n < 1) {
return 1;
}

int size = 2 * n - 1;

for (int i = 0; i < size; ++i) {
for (int j = 0; j < size; ++j) {
int top = i;
int left = j;
int bottom = size - 1 - i;
int right = size - 1 - j;

int layer = top;
if (left < layer) {
layer = left;
}
if (bottom < layer) {
layer = bottom;
}
if (right < layer) {
layer = right;
}

if (j > 0) {
putchar(' ');
}
printf("%d", layer + 1);
}
putchar('\n');
}

return 0;
}

复杂度与易错点

  • 时间复杂度:O(n^2)
  • 空间复杂度:O(1)
  • 找到位置与四条边界距离的关系后,不需要额外构造二维数组。

9. 消除游戏

题目

输入一个二维矩阵。若同一行或同一列中连续三个元素相等,则将这些元素全部变为 0。所有消除判断都基于原矩阵,在同一轮中同时生效。

示例输入:

1
2
3
4
3 5
2 5 1 1 1
5 2 2 2 1
3 5 6 4 1

示例输出:

1
2
3
2 5 0 0 0
5 0 0 0 0
3 5 6 4 0

思路

不能一边判断一边直接把元素清零,否则后面的判断会受到已经清零的数据影响。

更稳妥的方法是:

  1. 用一个布尔数组标记所有应被消除的位置;
  2. 全部标记完成后,再把对应位置设为 0;
  3. 最后输出矩阵。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <stdio.h>
#include <stdbool.h>

#define MAX_SIZE 100

int main(void) {
int rows;
int cols;

if (scanf("%d %d", &rows, &cols) != 2 ||
rows < 1 || rows > MAX_SIZE ||
cols < 1 || cols > MAX_SIZE) {
return 1;
}

int matrix[MAX_SIZE][MAX_SIZE];
bool removed[MAX_SIZE][MAX_SIZE] = {false};

for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (scanf("%d", &matrix[i][j]) != 1) {
return 1;
}
}
}

for (int i = 0; i < rows; ++i) {
for (int j = 1; j + 1 < cols; ++j) {
if (matrix[i][j - 1] == matrix[i][j] &&
matrix[i][j] == matrix[i][j + 1]) {
removed[i][j - 1] = true;
removed[i][j] = true;
removed[i][j + 1] = true;
}
}
}

for (int j = 0; j < cols; ++j) {
for (int i = 1; i + 1 < rows; ++i) {
if (matrix[i - 1][j] == matrix[i][j] &&
matrix[i][j] == matrix[i + 1][j]) {
removed[i - 1][j] = true;
removed[i][j] = true;
removed[i + 1][j] = true;
}
}
}

for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (removed[i][j]) {
matrix[i][j] = 0;
}

if (j > 0) {
putchar(' ');
}
printf("%d", matrix[i][j]);
}
putchar('\n');
}

return 0;
}

复杂度与易错点

  • 时间复杂度:O(rows × cols)
  • 空间复杂度:O(rows × cols)
  • 必须区分“标记”和“真正修改矩阵”两个阶段。
  • 行和列的判断应基于同一份原始数据。

小结

这 9 道题覆盖了 C 语言基础阶段最常见的几类问题:

类型 核心能力
循环与累加 把重复计算翻译成循环
数位拆解 使用整除和取模
分支判断 处理闰年、非法输入等特殊情况
数组操作 下标、遍历和边界控制
排序与去重 先排序,再利用相邻元素关系
二维数组 行列关系与矩阵运算
状态标记 先记录结果,再统一修改数据

真正写题解时,比起“代码能过样例”,更重要的是说明为什么这样设计、复杂度是多少,以及哪些输入容易出错。

  • Title: C 语言程序设计基础练习:循环、分支、数组与矩阵
  • Author: Eviarch
  • Created at : 2026-11-18 18:00:00
  • Updated at : 2026-11-18 18:00:00
  • Link: https://blog.eviarch.com/2026/11/18/C语言程序设计基础练习/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments