c语言选择法求是否有鞍点
发布网友
发布时间:2022-04-28 12:00
我来回答
共1个回答
热心网友
时间:2023-10-08 20:44
题主寻找鞍点的逻辑本身就是错的
重新给题主一份鞍点的代码吧
#include <stdio.h>
#include <conio.h>
#define LEN 50 /*数组长度*/
/*打印矩阵内容*/
void printMatrix (int matrix[LEN][LEN], int row, int col) {
int i,j;
for (i=0; i<row; i++) {
for (j=0; j<col; j++) {
printf ("%d\t", matrix[i][j]);
}
putchar ('\n');
}
putchar ('\n');
}
/*寻找矩阵马鞍点*/
/*马鞍点:矩阵元素在该行是最大值且在该列是最小值*/
/*当马鞍点存在时,其值赋予sp,其坐标赋予spRowIndex、spColIndex并返回1;否则返回0*/
int saddlePoint (int matrix[LEN][LEN], int row, int col, int *sp, int *spRowIndex, int *spColIndex) {
int i,j,k;
int spTemp,spRowIndexTemp,spColIndexTemp;
int flag; /*马鞍点是否存在的标识*/
for (i=0; i<row; i++) {
flag = 1;
/*寻找第i行的最大值*/
spTemp = matrix[i][0];
spRowIndexTemp = i;
spColIndexTemp = 0;
for (j=0; j<col; j++) {
if (matrix[i][j] > spTemp) {
spTemp = matrix[i][j];
spRowIndexTemp = i;
spColIndexTemp = j;
}
}
/*验证第i行最大值,在该列是否为最小值*/
for (k=0; k<row; k++) {
/*若不是最小值则跳出*/
if (matrix[k][spColIndexTemp] < spTemp) {
flag = 0;
break;
}
}
/*若是最小值则该元素为矩阵马鞍点,记录其值与坐标,并返回马鞍点存在信息*/
if (flag) {
*sp = spTemp;
*spRowIndex = spRowIndexTemp;
*spColIndex = spColIndexTemp;
return flag;
}
}
return flag;
}
int main (void) {
//int matrix[LEN][LEN];
int row, col;
int flag; /*马鞍点是否存在的标识*/
int sp, spRowIndex, spColIndex; /*马鞍点值与坐标*/
/*鞍点测试1:3,1行2列*/
row = 3; col = 3;
int matrix[LEN][LEN] = {{1,3,2},{4,7,5},{9,6,8}};
/*鞍点测试2:4,2行3列*/
//row = 3; col = 4;
//int matrix[LEN][LEN] = {{1,3,5,3},{2,3,4,1},{3,2,6,1}};
/*鞍点测试3:无鞍点*/
//row = 3; col = 3;
//int matrix[LEN][LEN] = {{4,3,7},{6,1,9},{9,2,5}};
printMatrix (matrix, row, col);
flag = saddlePoint (matrix, row, col, &sp, &spRowIndex, &spColIndex);
if (flag)
printf ("存在马鞍点[%d],位于第%d行%d列!\n", sp, spRowIndex, spColIndex);
else
printf ("矩阵无马鞍点!\n");
putchar ('\n');
getch ();
return 0;
}
运行结果
热心网友
时间:2023-10-08 20:44
题主寻找鞍点的逻辑本身就是错的
重新给题主一份鞍点的代码吧
#include <stdio.h>
#include <conio.h>
#define LEN 50 /*数组长度*/
/*打印矩阵内容*/
void printMatrix (int matrix[LEN][LEN], int row, int col) {
int i,j;
for (i=0; i<row; i++) {
for (j=0; j<col; j++) {
printf ("%d\t", matrix[i][j]);
}
putchar ('\n');
}
putchar ('\n');
}
/*寻找矩阵马鞍点*/
/*马鞍点:矩阵元素在该行是最大值且在该列是最小值*/
/*当马鞍点存在时,其值赋予sp,其坐标赋予spRowIndex、spColIndex并返回1;否则返回0*/
int saddlePoint (int matrix[LEN][LEN], int row, int col, int *sp, int *spRowIndex, int *spColIndex) {
int i,j,k;
int spTemp,spRowIndexTemp,spColIndexTemp;
int flag; /*马鞍点是否存在的标识*/
for (i=0; i<row; i++) {
flag = 1;
/*寻找第i行的最大值*/
spTemp = matrix[i][0];
spRowIndexTemp = i;
spColIndexTemp = 0;
for (j=0; j<col; j++) {
if (matrix[i][j] > spTemp) {
spTemp = matrix[i][j];
spRowIndexTemp = i;
spColIndexTemp = j;
}
}
/*验证第i行最大值,在该列是否为最小值*/
for (k=0; k<row; k++) {
/*若不是最小值则跳出*/
if (matrix[k][spColIndexTemp] < spTemp) {
flag = 0;
break;
}
}
/*若是最小值则该元素为矩阵马鞍点,记录其值与坐标,并返回马鞍点存在信息*/
if (flag) {
*sp = spTemp;
*spRowIndex = spRowIndexTemp;
*spColIndex = spColIndexTemp;
return flag;
}
}
return flag;
}
int main (void) {
//int matrix[LEN][LEN];
int row, col;
int flag; /*马鞍点是否存在的标识*/
int sp, spRowIndex, spColIndex; /*马鞍点值与坐标*/
/*鞍点测试1:3,1行2列*/
row = 3; col = 3;
int matrix[LEN][LEN] = {{1,3,2},{4,7,5},{9,6,8}};
/*鞍点测试2:4,2行3列*/
//row = 3; col = 4;
//int matrix[LEN][LEN] = {{1,3,5,3},{2,3,4,1},{3,2,6,1}};
/*鞍点测试3:无鞍点*/
//row = 3; col = 3;
//int matrix[LEN][LEN] = {{4,3,7},{6,1,9},{9,2,5}};
printMatrix (matrix, row, col);
flag = saddlePoint (matrix, row, col, &sp, &spRowIndex, &spColIndex);
if (flag)
printf ("存在马鞍点[%d],位于第%d行%d列!\n", sp, spRowIndex, spColIndex);
else
printf ("矩阵无马鞍点!\n");
putchar ('\n');
getch ();
return 0;
}
运行结果
热心网友
时间:2023-10-08 20:44
题主寻找鞍点的逻辑本身就是错的
重新给题主一份鞍点的代码吧
#include <stdio.h>
#include <conio.h>
#define LEN 50 /*数组长度*/
/*打印矩阵内容*/
void printMatrix (int matrix[LEN][LEN], int row, int col) {
int i,j;
for (i=0; i<row; i++) {
for (j=0; j<col; j++) {
printf ("%d\t", matrix[i][j]);
}
putchar ('\n');
}
putchar ('\n');
}
/*寻找矩阵马鞍点*/
/*马鞍点:矩阵元素在该行是最大值且在该列是最小值*/
/*当马鞍点存在时,其值赋予sp,其坐标赋予spRowIndex、spColIndex并返回1;否则返回0*/
int saddlePoint (int matrix[LEN][LEN], int row, int col, int *sp, int *spRowIndex, int *spColIndex) {
int i,j,k;
int spTemp,spRowIndexTemp,spColIndexTemp;
int flag; /*马鞍点是否存在的标识*/
for (i=0; i<row; i++) {
flag = 1;
/*寻找第i行的最大值*/
spTemp = matrix[i][0];
spRowIndexTemp = i;
spColIndexTemp = 0;
for (j=0; j<col; j++) {
if (matrix[i][j] > spTemp) {
spTemp = matrix[i][j];
spRowIndexTemp = i;
spColIndexTemp = j;
}
}
/*验证第i行最大值,在该列是否为最小值*/
for (k=0; k<row; k++) {
/*若不是最小值则跳出*/
if (matrix[k][spColIndexTemp] < spTemp) {
flag = 0;
break;
}
}
/*若是最小值则该元素为矩阵马鞍点,记录其值与坐标,并返回马鞍点存在信息*/
if (flag) {
*sp = spTemp;
*spRowIndex = spRowIndexTemp;
*spColIndex = spColIndexTemp;
return flag;
}
}
return flag;
}
int main (void) {
//int matrix[LEN][LEN];
int row, col;
int flag; /*马鞍点是否存在的标识*/
int sp, spRowIndex, spColIndex; /*马鞍点值与坐标*/
/*鞍点测试1:3,1行2列*/
row = 3; col = 3;
int matrix[LEN][LEN] = {{1,3,2},{4,7,5},{9,6,8}};
/*鞍点测试2:4,2行3列*/
//row = 3; col = 4;
//int matrix[LEN][LEN] = {{1,3,5,3},{2,3,4,1},{3,2,6,1}};
/*鞍点测试3:无鞍点*/
//row = 3; col = 3;
//int matrix[LEN][LEN] = {{4,3,7},{6,1,9},{9,2,5}};
printMatrix (matrix, row, col);
flag = saddlePoint (matrix, row, col, &sp, &spRowIndex, &spColIndex);
if (flag)
printf ("存在马鞍点[%d],位于第%d行%d列!\n", sp, spRowIndex, spColIndex);
else
printf ("矩阵无马鞍点!\n");
putchar ('\n');
getch ();
return 0;
}
运行结果