把一个有限数列a[1..m]转换成另一个数列b[1.n](m>=n)(200分)

  • 主题发起人 主题发起人 jiashenglv
  • 开始时间 开始时间
J

jiashenglv

Unregistered / Unconfirmed
GUEST, unregistred user!
条件是若a1>=0,则b1=a1;a1<0,则a1和随后的数a[2..k]相加,直到其和(sum[1,2..k])>=0之后则其和为b1;依此类推。如:
case 1、a={8,2,4,9,0,12,24},则b={8,2,4,9,0,12,24};
case 2、a={8,-2,-4,9,0,-12,24},则b={8,3,0,12};
//8>0,b1=8;-2+(-4)+9=3>0,b2=3;b3=0;b4=(-12+24)=12;
case 3、a={8,-2,-4,9,0,-12,-24},则b={8,3,0,-36};
//若按题目条件最后全部的数之和sum[i..m]<0,则b[last]=sum[i..m];
//8>0,b1=8;-2+(-4)+9=3>0,b2=3;b3=0;b4=(-12+(-24))=-36<0,但是数列已到结尾,b4=-36;
case 4、a={8,-2,-4,9,0,12,-24},则b={8,3,0,12,-24};
//8>0,b1=8;-2+(-4)+9=3>0,b2=3;b3=0;b4=12;b5=-24;

请给出算法和源程序。在线等待
 
在form上放一个TEdit和一个TButton的控件,当点击按钮时,在edit中显示结果。

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

TDArray = array of integer;

var
Form1: TForm1;
c: TDArray;

implementation

{$R *.dfm}

function trans(a: array of integer): TDArray;
var
b: array of integer;
l, i, j, n: integer;
begin
l := Length(a);
setLength(b, l);

j := 0;
n := 0;
for i := 0 to l - 1 do
begin
if (a < 0) or (n < 0) then
begin
n := n + a;
if (n >= 0) or (i = l - 1) then
begin
b[j] := n;
n := 0;
j := j + 1;
end;
end
else begin
b[j] := a;
j := j + 1;
end;
end;
move(b, c, SizeOf(c));
setLength(c, j);
result := c;
end;

procedure TForm1.Button1Click(Sender: TObject);
const
a: array [0..6] of integer = (8,-2,-4,9,0,12,-24);
var
p: TDArray;
i: integer;
s: string;
begin
p := trans(a);
s := '';
for i := 0 to Length(p) - 1 do
s := s + inttostr(p) + ' ';
edit1.Text := s;
end;

end.
 
自己已解决此问题.
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
853
import
I
后退
顶部