需要找到矩阵的鞍点,由用户通过键盘填充。
下面的代码是矩阵的格式,可以根据用户的意愿添加或删除行和列。我该如何输入元素并找到鞍点?
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Grids;
type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Edit1.Text:='3';
Edit2.Text:='3';
Button1Click(Sender);
end;
procedure TForm1.Button1Click(Sender: TObject);
var i,N,M:integer;
begin
N:=StrToInt(Edit1.Text);
M:=StrToInt(Edit2.Text);
for i:=1 To N Do
StringGrid1.Cells[i,0]:=IntToStr(i);
for i:=1 To M Do
StringGrid1.Cells[0,i]:=IntToStr(i);
StringGrid1.ColCount:=N+1;
StringGrid1.RowCount:=M+1;
StringGrid1.Cells[0,0]:='Matrix';
end;
end.
以Alvaro Montoro对Asserting if a position of a position is a saddle point in language作为参考的问题的回答
C
,并考虑到用户将在 StringGrid 中输入值,如您在问题中所述,我们将分几个步骤来解决问题。定义(取自上述问题:鞍点
我声明了一个数据类型来存储整数数组:
我声明了一个从中提取数据
StringGrid
并将其加载到类型变量中的方法TIntArray2
。如果用户没有正确输入数组,这将最小化字符串/整数转换并引发错误。需要注意的是,在解中,矩阵的第一维是行,第二维是列。
我声明了一个方法,该方法具有一个子函数,该函数根据已经提到的答案确定给定位置是否是鞍点。该方法通过作为参数给出的矩阵来寻找鞍点,当找到它们时,它会在备忘录中添加一条线来指示其坐标。
最后,在按钮的 OnClick 事件处理程序中,我进行了必要的调用来执行任务:
这是它在运行时的样子,输入与问题中给出的示例相同的值。