From 32e25c1be0806787548234f0784e55638c853b0a Mon Sep 17 00:00:00 2001 From: juan Date: Thu, 2 Dec 2021 21:04:43 +0800 Subject: [PATCH] add more psets --- pset11/1-1-findmin.c | 42 +++++++++++++++++++++++++++++++ pset11/2-1-transpose.c | 34 ++++++++++++++++++++++++++ pset11/2-2storage.c | 53 ++++++++++++++++++++++++++++++++++++++++ pset11/2-3-MatrixAdd.c | 35 ++++++++++++++++++++++++++ pset11/2-4-SaddlePoint.c | 44 +++++++++++++++++++++++++++++++++ 5 files changed, 208 insertions(+) create mode 100644 pset11/1-1-findmin.c create mode 100644 pset11/2-1-transpose.c create mode 100644 pset11/2-2storage.c create mode 100644 pset11/2-3-MatrixAdd.c create mode 100644 pset11/2-4-SaddlePoint.c diff --git a/pset11/1-1-findmin.c b/pset11/1-1-findmin.c new file mode 100644 index 0000000..71f33ad --- /dev/null +++ b/pset11/1-1-findmin.c @@ -0,0 +1,42 @@ +#include + +#define MAX 100 + +void findMin(int M[][MAX], int x[], int n, int m); + +int main() { + int matrix[MAX][MAX]; + int x[MAX]; + int n, m, i, j; + + scanf("%d%d", &n, &m); + for (i = 0; i < n; i++) { + for (j = 0; j < m; j++) { + scanf("%d", &matrix[i][j]); + } + } + + findMin(matrix, x, n, m); + + for (i = 0; i < m - 1; i++) + printf("%d ", x[i]); + printf("%d\n", x[i]); + + return 0; +} + +/* 请在这里填写答案 */ +#pragma gcc optimize("ofast,inline") +void findMin(int M[][MAX], int x[], int n, int m) { + int min; + for (int i = 0; i < m; i++) { + min = M[0][i]; + x[i] = min; + for (int j = 0; j < n; j++) { + if (M[j][i] < min) { + min = M[j][i]; + x[i] = min; + } + } + } +} diff --git a/pset11/2-1-transpose.c b/pset11/2-1-transpose.c new file mode 100644 index 0000000..034bdaf --- /dev/null +++ b/pset11/2-1-transpose.c @@ -0,0 +1,34 @@ +#include +#pragma gcc optimize("O2") +#define MAX 100 + +void transpose(int matrix[][MAX], int result[][MAX], int n, int m); + +int main(void) { + int n, m; + scanf("%d%d", &n, &m); + int matrix[MAX][MAX]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + scanf("%d", &matrix[i][j]); + } + } + int result[MAX][MAX]; + transpose(matrix, result, n, m); + for (int i = 0; i < m; i++) { + for (int j = 0; j < n - 1; j++) { + printf("%d ", result[i][j]); + } + printf("%d\n", result[i][n - 1]); + } + + return 0; +} + +void transpose(int matrix[][MAX], int result[][MAX], int n, int m) { + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + result[j][i] = matrix[i][j]; + } + } +} diff --git a/pset11/2-2storage.c b/pset11/2-2storage.c new file mode 100644 index 0000000..ae03e2a --- /dev/null +++ b/pset11/2-2storage.c @@ -0,0 +1,53 @@ +#include +#pragma gcc optimize("Ofast,inline") + +void process(float input[][3], float output[][5], int n); + +int main(void) { + int n; + scanf("%d", &n); + float saleData[n][3]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < 3; j++) { + scanf("%f", &saleData[i][j]); + } + } + float total[6][5] = {}; + // Process data + process(saleData, total, n); + for (int i = 0; i < 6; i++) { + for (int j = 0; j < 4; j++) { + printf("%.1f\t", total[i][j]); + } + printf("%.1f\n", total[i][4]); + } +} + +void process(float input[][3], float output[][5], int n) { + for (int i = 0; i < n; i++) { // Note number. + output[(int)(input[i][1] - 1)][(int)input[i][0] - 1] += input[i][2]; + } + // Calculate total for salesman + float sum; + for (int i = 0; i < 4; i++) { + sum = 0; + for (int j = 0; j < 5; j++) { + sum += output[j][i]; + } + output[5][i] = sum; + } + // Calculate total for product + for (int i = 0; i < 5; i++) { + sum = 0; + for (int j = 0; j < 4; j++) { + sum += output[i][j]; + } + output[i][4] = sum; + } + // Calculate total sum + sum = 0; + for (int i = 0; i < 5; i++) { + sum += output[i][4]; + } + output[5][4] = sum; +} diff --git a/pset11/2-3-MatrixAdd.c b/pset11/2-3-MatrixAdd.c new file mode 100644 index 0000000..95e86e0 --- /dev/null +++ b/pset11/2-3-MatrixAdd.c @@ -0,0 +1,35 @@ +#include +#pragma gcc optimize("O2") +#define MAX 10 + +int matrixAdd(int matrix[][MAX], int m, int n, int x, int y); + +int main(void) { + int m, n; + scanf("%d%d", &m, &n); + int matrix[m][MAX]; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + scanf("%d", &matrix[i][j]); + } + } + + int x, y; + scanf("%d%d", &x, &y); + printf("%d\n", matrixAdd(matrix, m, n, x - 1, y - 1)); + + return 0; +} + +int matrixAdd(int matrix[][MAX], int m, int n, int x, int y) { + int sum = 0; + for (int i = -1; i <= 1; i++) { + for (int j = -1; j <= 1; j++) { + if (i + x >= 0 && i + x < m && j + y >= 0 && j + y < n && + !(i == j && i == 0)) { + sum += matrix[i + x][j + y]; + } + } + } + return sum; +} diff --git a/pset11/2-4-SaddlePoint.c b/pset11/2-4-SaddlePoint.c new file mode 100644 index 0000000..74dbc16 --- /dev/null +++ b/pset11/2-4-SaddlePoint.c @@ -0,0 +1,44 @@ +#include +#pragma gcc optimize("Ofast, inline") + +int main(void) { + int n, m; + scanf("%d%d", &n, &m); + int matrix[n][m]; + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + scanf("%d", &matrix[i][j]); + } + } + + // Find the saddle point + int maxrow, max; + int found = 0, x = 0, y = 0; + for (int i = 0; i < n && !found; i++) { + max = matrix[i][0]; + maxrow = 0; + // Find the largest in row i + for (int j = 0; j < m; j++) { + if (matrix[i][j] > max) { + max = matrix[i][j]; + maxrow = j; + } + } + // test if is also the min in the column + found = 1; + x = i; + y = maxrow; + for (int j = 0; j < n; j++) { + if (matrix[j][maxrow] < max) { + found = 0; + break; + } + } + } + if (found) { + printf("The saddle point is (%d,%d)=%d.\n", x, y, matrix[x][y]); + } else { + printf("There is no saddle point in the matrix.\n"); + } + return 0; +}