From b3f466c6edfca070d6020c2fa892f6a4218482db Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Fri, 29 Nov 2024 16:54:06 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=AE=9E=E8=B7=B5=E8=AF=BE=E5=A4=96?= =?UTF-8?q?=E3=80=9113.=E6=8C=87=E9=92=881/7-1=20=E6=B1=82=E7=9F=A9?= =?UTF-8?q?=E9=98=B5=E6=AF=8F=E8=A1=8C=E5=85=83=E7=B4=A0=E7=9A=84=E5=92=8C?= =?UTF-8?q?.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 【实践课外】13.指针1/7-1 求矩阵每行元素的和.c | 37 +++++++++++++++++++ .../7-1 求矩阵每行元素的和.md | 29 +++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 【实践课外】13.指针1/7-1 求矩阵每行元素的和.c create mode 100644 【实践课外】13.指针1/7-1 求矩阵每行元素的和.md diff --git a/【实践课外】13.指针1/7-1 求矩阵每行元素的和.c b/【实践课外】13.指针1/7-1 求矩阵每行元素的和.c new file mode 100644 index 0000000..4389dae --- /dev/null +++ b/【实践课外】13.指针1/7-1 求矩阵每行元素的和.c @@ -0,0 +1,37 @@ +#include + +int row_sum(int *a, int n) { + int s = 0; + + for (int i = 0; i < n; i++) { + s += a[i]; + } + + return s; +} + +void mat_sum(int mat[][10], int n, int m, int *sum) { + for (int i = 0; i < n; i++) { + sum[i] = row_sum(mat[i], m); + } +} + +int main() { + int n, m, mat[10][10], sum[10]; + + scanf("%d%d", &n, &m); + + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + scanf("%d", &mat[i][j]); + } + } + + mat_sum(mat, n, m, sum); + + for (int i = 0; i < n; i++) { + printf("%d\n", sum[i]); + } + + return 0; +} diff --git a/【实践课外】13.指针1/7-1 求矩阵每行元素的和.md b/【实践课外】13.指针1/7-1 求矩阵每行元素的和.md new file mode 100644 index 0000000..d148108 --- /dev/null +++ b/【实践课外】13.指针1/7-1 求矩阵每行元素的和.md @@ -0,0 +1,29 @@ +# 7-1 求矩阵每行元素的和 + +本题要求编写程序,使用指针方式求一个给定的m×n矩阵各行元素之和。 + +### 输入格式: + +输入第一行给出两个正整数m和n(1≤m,n≤6),再输入m行数据,每行n个整数,每个整数之间用空格分隔。 + +### 输出格式: + +输出m个整数,每行1个数。 + +### 输入样例: + + + +```in +2 3 +1 2 3 +4 5 6 +``` + +### 输出样例: + + +```out +6 +15 +```