###### qq_34341918

2016-03-19 02:52 浏览 1.7k

# C语言相邻数求最大和的问题求解答

#include
int** matrix(int row) {
int** matr;
matr = malloc(row*sizeof(int*));
for(int i = 0; i < row; i++) {
matr[i] = malloc(row*sizeof(int));
}
return matr;

}

void get_input(int** arrays, int row) {
for(int i = 0; i < row; i++) {
for(int j = 0; j < row; j++) {
scanf("%d", &arrays[i][j]);
}
}

}

int compareFunction(const void *a,const void *b) {

``````return *(int *)b - *(int *)a;
``````

}

int compareBigger(int *diagline, int maxium) {
if(diagline[0] >= maxium) {
maxium = diagline[0];
}
return maxium;
}

void rowdirection(int *straightline, int num, int rows, int **mat) {
int i = 0;
for(int row = 0; row < rows; row++) {
for(int column = 0; column < rows - num + 1; column++ ) {
for(int j = 0; j < num; j++) {
straightline[i] += mat[row][column + j];
}i++;
}
}
}

void coldirection(int *straightline, int num, int rows, int **mat) {
int i = 0;
for(int column = 0; column < rows; column++) {
for(int row = 0; row < rows - num + 1; row++ ) {
for(int j = 0; j < num; j++) {
straightline[i] += mat[row + j][column];
}i++;
}
}
}

void diagdirection(int *diagline, int num, int rows, int **mat) {

``````int i = 0;
for(int row = 0; row < rows - num + 1; row++ ) {
for(int col = 0; col < rows - num + 1; col++ ) {
for(int j = 0; j < num; j++) {
diagline[i] += mat[row + j][col + j];
}i++;
//printf("%d\n", diagline[i]);
}
}
``````

}

void antidiagdirection(int *diagline, int num, int rows, int **mat) {

``````int i = 0;
for(int row = 0; row < rows - num + 1; row++ ) {
for(int col = num - 1; col < rows; col++ ) {
for(int j = 0; j < num; j++) {
diagline[i] += mat[row + j][col - j];
}i++;
//printf("%d\n", diagline[i]);
}
}
``````

}

void main() {

``````int maxium = 0;

int row, num;
scanf("%d %d\n", &row, &num);
int straightnum = row*(row - num + 1);
int diagnum = (row - num + 1)*(row - num + 1);
int **mat = matrix(row);

get_input(mat, row);

int *straightline1 = (int *)malloc(straightnum*sizeof(int));
rowdirection(straightline1, num, row, mat);
qsort(straightline1, straightnum, sizeof(int), compareFunction);
maxium = compareBigger(straightline1, maxium);
//printf("%d\n", maxium);

int *straightline2 = (int *)malloc(straightnum*sizeof(int));
coldirection(straightline2, num, row, mat);
qsort(straightline2, straightnum, sizeof(int), compareFunction);
maxium = compareBigger(straightline2, maxium);
//printf("%d\n", maxium);

int *diagline1 = (int *)malloc(diagnum*sizeof(int));

diagdirection(diagline1, num, row, mat);
qsort(diagline1, diagnum , sizeof(int), compareFunction);
maxium = compareBigger(diagline1, maxium);

//printf("%d\n", maxium);

int *diagline2 = (int *)malloc(diagnum*sizeof(int));

antidiagdirection(diagline2, num, row, mat);
qsort(diagline2, diagnum , sizeof(int), compareFunction);
maxium = compareBigger(diagline2, maxium);

//printf("%d\n", maxium);

printf("%d", maxium);

free(straightline1);
free(straightline2);
free(diagline1);
free(diagline2);
free(mat);
``````

}

``````
``````

• 点赞
• 写回答
• 关注问题
• 收藏
• 复制链接分享
• 邀请回答

#### 1条回答默认 最新

• qq_34341918 2016-03-19 02:53

#include
int** matrix(int row) {
int** matr;
matr = malloc(row*sizeof(int*));
for(int i = 0; i < row; i++) {
matr[i] = malloc(row*sizeof(int));
}
return matr;

}

void get_input(int** arrays, int row) {
for(int i = 0; i < row; i++) {
for(int j = 0; j < row; j++) {
scanf("%d", &arrays[i][j]);
}
}

}

int compareFunction(const void *a,const void *b) {

``````return *(int *)b - *(int *)a;
``````

}

int compareBigger(int *diagline, int maxium) {
if(diagline[0] >= maxium) {
maxium = diagline[0];
}
return maxium;
}

void rowdirection(int *straightline, int num, int rows, int **mat) {
int i = 0;
for(int row = 0; row < rows; row++) {
for(int column = 0; column < rows - num + 1; column++ ) {
for(int j = 0; j < num; j++) {
straightline[i] += mat[row][column + j];
}i++;
}
}
}

void coldirection(int *straightline, int num, int rows, int **mat) {
int i = 0;
for(int column = 0; column < rows; column++) {
for(int row = 0; row < rows - num + 1; row++ ) {
for(int j = 0; j < num; j++) {
straightline[i] += mat[row + j][column];
}i++;
}
}
}

void diagdirection(int *diagline, int num, int rows, int **mat) {

``````int i = 0;
for(int row = 0; row < rows - num + 1; row++ ) {
for(int col = 0; col < rows - num + 1; col++ ) {
for(int j = 0; j < num; j++) {
diagline[i] += mat[row + j][col + j];
}i++;
//printf("%d\n", diagline[i]);
}
}
``````

}

void antidiagdirection(int *diagline, int num, int rows, int **mat) {

``````int i = 0;
for(int row = 0; row < rows - num + 1; row++ ) {
for(int col = num - 1; col < rows; col++ ) {
for(int j = 0; j < num; j++) {
diagline[i] += mat[row + j][col - j];
}i++;
//printf("%d\n", diagline[i]);
}
}
``````

}

void main() {

``````int maxium = 0;

int row, num;
scanf("%d %d\n", &row, &num);
int straightnum = row*(row - num + 1);
int diagnum = (row - num + 1)*(row - num + 1);
int **mat = matrix(row);

get_input(mat, row);

int *straightline1 = (int *)malloc(straightnum*sizeof(int));
rowdirection(straightline1, num, row, mat);
qsort(straightline1, straightnum, sizeof(int), compareFunction);
maxium = compareBigger(straightline1, maxium);
//printf("%d\n", maxium);

int *straightline2 = (int *)malloc(straightnum*sizeof(int));
coldirection(straightline2, num, row, mat);
qsort(straightline2, straightnum, sizeof(int), compareFunction);
maxium = compareBigger(straightline2, maxium);
//printf("%d\n", maxium);

int *diagline1 = (int *)malloc(diagnum*sizeof(int));

diagdirection(diagline1, num, row, mat);
qsort(diagline1, diagnum , sizeof(int), compareFunction);
maxium = compareBigger(diagline1, maxium);

//printf("%d\n", maxium);

int *diagline2 = (int *)malloc(diagnum*sizeof(int));

antidiagdirection(diagline2, num, row, mat);
qsort(diagline2, diagnum , sizeof(int), compareFunction);
maxium = compareBigger(diagline2, maxium);

//printf("%d\n", maxium);

printf("%d", maxium);

free(straightline1);
free(straightline2);
free(diagline1);
free(diagline2);
free(mat);
``````

}

``````
``````
点赞 评论 复制链接分享