问题描述
- 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("%dn", 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("%dn", diagline[i]); } }
}
void main() {
int maxium = 0; int row, num; scanf("%d %dn", &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("%dn", maxium); int *straightline2 = (int *)malloc(straightnum*sizeof(int)); coldirection(straightline2, num, row, mat); qsort(straightline2, straightnum, sizeof(int), compareFunction); maxium = compareBigger(straightline2, maxium); //printf("%dn", maxium); int *diagline1 = (int *)malloc(diagnum*sizeof(int)); diagdirection(diagline1, num, row, mat); qsort(diagline1, diagnum , sizeof(int), compareFunction); maxium = compareBigger(diagline1, maxium); //printf("%dn", maxium); int *diagline2 = (int *)malloc(diagnum*sizeof(int)); antidiagdirection(diagline2, num, row, mat); qsort(diagline2, diagnum , sizeof(int), compareFunction); maxium = compareBigger(diagline2, maxium); //printf("%dn", maxium); printf("%d", maxium); free(straightline1); free(straightline2); free(diagline1); free(diagline2); free(mat);
}
学校的作业,input是第一行 两个int, 一个是 下面的表的行列数(方阵),一个是用来规定有多少个相邻数来求最大和。
我用的是四种function把所有情况都写进四个指针,初始化maxium=0,然后指针内部降序排序,四次依次询问是否大于maxium,最后输出maxium。
问题是有些未知的test(test文件老师不会公开给我们)是wrong answer,菜鸟如我找不出那个情况。
题目有行数不超过100,求和个数不超过行数,每个数字不大于1000的限制,
然后test的工具可以忽略free的问题(我还没来得及写,但是free不会造成wrong answer)
求大神指点
代码楼下附加
解决方案
#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("%dn", 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("%dn", diagline[i]);
}
}
}
void main() {
int maxium = 0;
int row, num;
scanf("%d %dn", &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("%dn", maxium);
int *straightline2 = (int *)malloc(straightnum*sizeof(int));
coldirection(straightline2, num, row, mat);
qsort(straightline2, straightnum, sizeof(int), compareFunction);
maxium = compareBigger(straightline2, maxium);
//printf("%dn", maxium);
int *diagline1 = (int *)malloc(diagnum*sizeof(int));
diagdirection(diagline1, num, row, mat);
qsort(diagline1, diagnum , sizeof(int), compareFunction);
maxium = compareBigger(diagline1, maxium);
//printf("%dn", maxium);
int *diagline2 = (int *)malloc(diagnum*sizeof(int));
antidiagdirection(diagline2, num, row, mat);
qsort(diagline2, diagnum , sizeof(int), compareFunction);
maxium = compareBigger(diagline2, maxium);
//printf("%dn", maxium);
printf("%d", maxium);
free(straightline1);
free(straightline2);
free(diagline1);
free(diagline2);
free(mat);
}