一个500*500数组 ,初始值为0,其中随机位置赋一些随机非0值, 怎样求非0的值的最大最小的行与列的坐标,就是非零值所在的范围?

解决方案 »

  1.   

    遍历一次,用四个变量,分别记录最小行、最大行、最小列、最大列
    每次都比较这四个值
    拿最大行nMaxRow举例,如果当前行值比当前的nMaxRow大,则nMaxRow取当前行值
      

  2.   


    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.
      

  3.   

    dinoalex的程序时求出最大值的行列, 我的想法是 求出任意非零值的行和列的最大最小值,youthon的说法好像不错,但是能说的具体一些吗? 例如怎样求出某一列的非零值的最大列数?然后才可以做比较啊
      

  4.   

    if ARow = 1 then  // <--- 你要哪行就哪行.也可以 if ARow = QueryRow then // QueryRow是你定的变量
      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;
      

  5.   


    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;
      

  6.   

    你终于说明白要什么
    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;
    同理也可求最小的
      

  7.   


    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.
      

  8.   

    for h:= 0 to clientHeight-1 do
    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;              这个图片时黑色背景中间有一个红色圆球,为什么取得的圆球的坐标不正确