在网上看到一个求鞍点的方法,你看下
C语言求鞍点代码
#include "stdio.h"
#include "conio.h"
#include "malloc.h"
#define TRUE 1
#define FALSE 0
#define OK 0
#define ERROR 1
#define MAXX 80
void Print(int * const pMatrix, const int m, const int n);
void Input(const int * const pm, const int * const pn);
void CreatTureMatrix(int ** const pMatrix,int ** const pTrueMatrixconst, const int m, const int n);
OutPrint(int ** const pMatrix, int ** const pTrueMatrix, const int m, const int n );
int main(void)
{
system("cls");
{
const int m = FALSE, n = FALSE;
Input(&m, &n);
{
int * pMatrix = NULL, * pTrueMatrix = NULL;
CreatTureMatrix(&pMatrix, &pTrueMatrix, m, n);
printf("\nMatrix is :\n");
Print(pMatrix, m , n);
printf("\nSaddle point Ture Matrix is :\n");
Print(pTrueMatrix, m, n);
OutPrint(&pMatrix, &pTrueMatrix, m, n);
}
}
getch();
return (OK);
}
void Print(int * const pMatrix, const int m, const int n)
{
int * p = NULL;
for(p = pMatrix; p < pMatrix + m*n; ++p)
{
printf("%5d", *p);
if( !( (p - pMatrix)%n- (n-1) ) )
{printf("\n"); }
}
}
void Input(const int * const pm,const int * const pn)
{
printf("Please enter a matrix of rows, columns: ");
{
int flag = TRUE;
while(flag)
{
if(scanf("%d%d", pm, pn) - 2)
{
flag = TRUE;
printf("Worry enter,retry!\n");
fflush(stdin);
}
else if( (*pm<=0 || *pm>=10) && (*pn<=0) || (*pn>=10) )
{
flag = TRUE;
printf("Enter Big or small,retry!\n");
}
else
{flag = FALSE;}
}
}
}
void CreatTureMatrix(int ** const pMatrix,int ** const pTrueMatrix, const int m, const int n)
{
*pMatrix = (int *)malloc( m*n*sizeof(int) );
*pTrueMatrix = (int *)calloc( m*n,sizeof(int) );
{
int *p = NULL;
for(p = *pMatrix; p < *pMatrix + m*n; ++p)
{scanf("%d",p);}
}
{
int * p = NULL;
for(p = *pMatrix; p < *pMatrix + m*n; p += n)
{
int * pMaxj = p;
{
int * q = NULL;
for(q = p + 1; q < p + n; ++q)
{
if(*q > *pMaxj)
{pMaxj = q;}
}
}
{
int * q = NULL;
for(q = pMaxj; q < p + n; ++q) /*此处处理所有(不严格)最大值*/
{
if( !(*q - *pMaxj) )
{
int * r = NULL;
for(
r = *pMatrix + (q - *pMatrix)%n
;(r < *pMatrix + m*n) && (*r >= *pMaxj)
;r += n
);
if( r >= (*pMatrix + m*n) )
{*(*pTrueMatrix + (q - *pMatrix)) = 1; }
}
}
}
}
}
}
OutPrint(int ** const pMatrix, int ** const pTrueMatrix, const int m, const int n )
{
int count = 0;
int * p = NULL;
printf("\nSaddle point is :\n");
for(p = *pTrueMatrix; p < *pTrueMatrix + m*n; ++p)
{
if(*p)
{
printf("Matrix[%d][%d] = %3d, "
, (p - *pTrueMatrix)/n, (p - *pTrueMatrix)%n
, *(*pMatrix + (p - *pTrueMatrix)) );
++count;
}
}
free(*pMatrix);
*pMatrix = NULL;
free(*pTrueMatrix);
*pTrueMatrix = NULL;
if(count)
{
const int xPos = wherex(), yPos = wherey();
if(xPos - 1)
{gotoxy(xPos - 2, yPos); }
else
{gotoxy(MAXX - 1, yPos - 1); }
printf(".");
} /*此处TC一类特有的函数gotoxy()*/
else
{printf("It is not exist!\n"); }
}