请问:怎样才能获得一个数字集合中的最大值和最小值,最好是函数!

解决方案 »

  1.   

    var m:set of 1..5;
      i:integer;
    begin
     m:=[5,2,3];
     for i:=1 to 5 do
       if (i in m) then
       begin
        showmessage('min='+inttostr(i));
        break;
       end;
     for i:=5 downto 1 do
       if (i in m ) then
       begin
         showmessage('max='+inttostr(i));
         break;
       end;
    end;
      

  2.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      m:array[1..5]of Integer;
      i,max,min:Integer;
    begin
      for i:=1 to 5 do
        begin
          Randomize;
          m[i]:=Random(i*100);
        end;
        max:=m[1];
        min:=m[1];
        for i:=2 to 5 do
          begin
            if max<m[i] then max:=m[i];
            if min>m[i] then min:=m[i];
          end;
      ShowMessage('Max='+IntToStr(max)+',Min='+IntToStr(min));
    end;
      

  3.   

    如果是数组就很好办
    MaxIntValue(Const Date : array of integer):Integer;//返回一个整数范围的最大的有符号值。
    MinIntValue(Const Date : array of Double):Integer;//返回一个整数数组中最小的有符号的数。
    MaxValue(Const Date : array of Double):Double;//返回在Date数组中最大的有符号值。
    MinValue(Const Date : array of Double):Integer;//返回一个数组中最小的有符号的数。
      

  4.   

    for  0 to 255
     in 吧
      

  5.   

    谢谢回复!我要找的是一个函数,只要一个函数就能解决问题(API也可以),如MaxValue,MinValue 这样的,象这样自己做一个过程遍历集合内部的每个元素我也懂!继续寻找高手!
      

  6.   


    var
      m:set of 1..5;
      max,min:integer;
    begin
      m:=[5,2,3];
      min:=1;
      max:=5;
      while not (min in m) do inc(min);
      while not (max in m) do dec(max);
      showmessage('max='+inttostr(max)+' min='+inttostr(min));
    end;
      

  7.   

    uses Math;
    数组:MaxValue