一个500*500数组 ,初始值为0,其中随机位置赋一些随机非0值, 怎样求非0的值的最大最小的行与列的坐标,就是非零值所在的范围?
解决方案 »
- 冬至尽 留别
- 求在listview中嵌入progressbar的方法
- 服务的两个事件
- 怎么样在主Exe和动态调用的Package中使用和共享全局变量参数?
- 问题来了
- 出现Access violation at address 0000001A. Read of address 0000001A.的错误,help!!
- 32bitDelphi深度历险被黑了?
- DriveComboBox1的问题???
- 请问哪里有TIECache下??论坛上面以前说的站点根本进不去:(,谢谢
- 北京的,都可以来瞧一下,来则有分‘’‘’
- 一个有趣的问题!如何使程序可以调用文本函数!
- ACTIVEX LIBRAY和普通DLL有什么区别?
每次都比较这四个值
拿最大行nMaxRow举例,如果当前行值比当前的nMaxRow大,则nMaxRow取当前行值
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls;type
TForm1 = class(TForm)
SG1: TStringGrid;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure SG1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
MAXV,x,y: integer;
end;var
Form1: TForm1;
implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
SG1.Cells[1,1]:= '8';
SG1.Cells[2,1]:= '51';
SG1.Cells[2,2]:= '12';
SG1.Cells[3,1]:= '11';
SG1.Cells[3,2]:= '13';
SG1.Cells[3,3]:= '33';
MAXV:=0;
end;procedure TForm1.SG1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if trim(SG1.Cells[ACol,ARow]) <> '' then
if strtoint(SG1.Cells[ACol,ARow]) > MAXV then
begin
MAXV := strtoint(SG1.Cells[ACol,ARow]);
x:= ACol;
y:= ARow;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
showmessage(inttostr(x)+' '+inttostr(y));
end;end.
if trim(SG1.Cells[ACol,ARow]) <> '' then
if strtoint(SG1.Cells[ACol,ARow]) > MAXV then
begin
MAXV := strtoint(SG1.Cells[ACol,ARow]);
x:= ACol;
y:= ARow;
end;
procedure getMinAndMax;
var
maxrow, minrow, maxcol, mincol: integer;
begin
maxrow := -1;
minrow := 500+1;
maxcol := -1;
mincol := 500+1;
for i := 0 to length(a) - 1 do
for j := 0 length(a[i]) - 1 do
begin
if a[i][j] <> 0 then
begin
if i < minrow then
minrow = i;
if i > maxrow then
maxrow = i;
if j < mincol then
j = mincol;
if j > maxcol then
j = maxcol;
end;
end;
end;
procedure TForm1.SG1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if trim(SG1.Cells[ACol,ARow]) <> '' then
if strtoint(SG1.Cells[ACol,ARow]) <> 0 then
begin
if ACol > x then x:= ACol;
if ARow > y then y:= ARow;
end;
end;
同理也可求最小的
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls;type
TForm1 = class(TForm)
SG1: TStringGrid;
Button1: TButton;
OpenDialog1: TOpenDialog;
procedure FormCreate(Sender: TObject);
procedure SG1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
x,y,xx,yy: integer;
end;var
Form1: TForm1;
implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
var
i,j: integer;
begin
SG1.Cells[2,1]:= '51';
SG1.Cells[2,2]:= '12';
SG1.Cells[3,1]:= '11';
SG1.Cells[3,2]:= '13';
SG1.Cells[3,3]:= '33';
for i:= 1 to 4 do
for j:= 1 to 4 do
if SG1.Cells[i,j] = '' then SG1.Cells[i,j] := '0';
xx:= SG1.ColCount;
yy:= SG1.RowCount;
end;procedure TForm1.SG1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if trim(SG1.Cells[ACol,ARow]) <> '' then
if strtoint(SG1.Cells[ACol,ARow]) <> 0 then
begin
if ACol < xx then xx:= ACol;
if ARow < yy then yy:= ARow;
if ACol > x then x:= ACol;
if ARow > y then y:= ARow;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
showmessage('最小的: '+inttostr(xx)+','+inttostr(yy)+#13+
'最大的: '+inttostr(x)+','+inttostr(y));
end;end.
begin
for w:=0 to clientWidth-1 do
begin
if tbm.Canvas.Pixels[h,w]<>clblack then
begin
if w<zuo then
zuo:=w;
if w>you then
you:= w; //找出非黑像素的范围
if h<xia then
xia:=h;
if h>shang then
shang:=h;
end;
end;
end; 这个图片时黑色背景中间有一个红色圆球,为什么取得的圆球的坐标不正确