求最大值和最小值在一组数据中的位置 ( 积分: 100 )

  • 主题发起人 主题发起人 tjcjh
  • 开始时间 开始时间
T

tjcjh

Unregistered / Unconfirmed
GUEST, unregistred user!
一组数据,若画图的话其实就是有若干个最大值和最小值的正弦波形,帮忙写一段程序得到各个最大值和最小值在这一组数据中的位置,谢谢!
 
一组数据,若画图的话其实就是有若干个最大值和最小值的正弦波形,帮忙写一段程序得到各个最大值和最小值在这一组数据中的位置,谢谢!
 
判断当前点前后两个点的值,如果其前后两点的值都比它大那么它就是一个最小值
如果其前后两点都比她小那么它就是一个最大值
 
利用tstringlist的sort功能得到最大和最小值,然后利用tstringlist的IndexOf('')得到最大最小的位置,当然要用两个tstringlist,一个保存原来的值,另一个保存sort后的值
 
Max := 0;
Min := 0;
for I := 1 to Length(S) do
begin
if S > Max then Max := S;
if S < Min then Min := S
end;
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TMyarray = array[0..20] of integer;

TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);

private
Procedure GetMaxMinPosition(MyArray : TMyArray; var MaxPosition,MinPosition
: integer);
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.GetMaxMinPosition(MyArray: TMyArray; var MaxPosition,
MinPosition: integer);
var
i,Max,Min : integer;
begin
MaxPosition := low(MyArray);
MinPosition := low(MyArray);
Max := MyArray[low(MyArray)];
Min := MyArray[low(MyArray)];
for i := low(MyArray) to High(MyArray) do
begin
if MyArray>Max then
begin
MaxPosition := i;
Max := MyArray;
Continue;
end;
if MyArray<Min then
begin
MinPosition := i;
Min := MyArray;
end;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
myarray : TMyArray;
i : integer;
Maxp,Minp :integer;
myarraystr : string;
begin
Maxp := -1 ;
Minp := -1 ;
myarraystr := '';
for i := low(myarray) to high(myarray) do
begin
myarray := random(100);
myarraystr := myarraystr + inttostr(myarray) +',';
end;
myarraystr := copy(myarraystr,1,length(myarraystr)-1);
showmessage(myarraystr);
GetMaxMinPosition(Myarray,Maxp,Minp);
showmessage('Max number position is 【 ' + inttostr(Maxp) + ' 】');
showmessage('Min number position is 【 ' + inttostr(Minp) + ' 】');
end;

end.
 
谢谢各位。
 
不好意思,结贴的时候没看到ak_2004的回复,这一次没办法了只好今后补吧。
 

Similar threads

S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
930
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部