add more psets
This commit is contained in:
parent
954a053200
commit
32e25c1be0
42
pset11/1-1-findmin.c
Normal file
42
pset11/1-1-findmin.c
Normal file
|
@ -0,0 +1,42 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
34
pset11/2-1-transpose.c
Normal file
34
pset11/2-1-transpose.c
Normal file
|
@ -0,0 +1,34 @@
|
|||
#include <stdio.h>
|
||||
#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];
|
||||
}
|
||||
}
|
||||
}
|
53
pset11/2-2storage.c
Normal file
53
pset11/2-2storage.c
Normal file
|
@ -0,0 +1,53 @@
|
|||
#include <stdio.h>
|
||||
#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;
|
||||
}
|
35
pset11/2-3-MatrixAdd.c
Normal file
35
pset11/2-3-MatrixAdd.c
Normal file
|
@ -0,0 +1,35 @@
|
|||
#include <stdio.h>
|
||||
#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;
|
||||
}
|
44
pset11/2-4-SaddlePoint.c
Normal file
44
pset11/2-4-SaddlePoint.c
Normal file
|
@ -0,0 +1,44 @@
|
|||
#include <stdio.h>
|
||||
#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;
|
||||
}
|
Loading…
Reference in a new issue