用delphi实现s=1+(1+2)+(1+2+3)+(1+2+3+4)+...+(1+2+...+100)的值
5个回答
推荐于2017-10-25 · 知道合伙人软件行家
关注
展开全部
试编写代码如下:
function TForm1.DoAdd(iMax:Integer):Integer;
var
i: Integer;
s: string;
begin
Result := 0;
s := '';
for i := 1 to iMax do
begin
Result := Result + i;
s := s + '+' + IntToStr(i);
end;
Delete(s,1,1);
s := s + '=' + IntToStr(Result);
mmo1.Lines.Add(s);
end;
procedure TForm1.btn1Click(Sender: TObject);
var
i, i1, iTotal: Integer;
s: string;
begin
iTotal := 0;
for i := 1 to 100 do
begin
iTotal := iTotal + DoAdd(i);
mmo1.Lines.Add(Format('%d >> %d',[i, iTotal]));
mmo1.Lines.Add('');
end;
end;
运行截图:
展开全部
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
function SumTotal(ACnt:Integer):Integer;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
var
i,iCnt,iSum:Integer;
begin
iCnt:=StrToInt(Edit1.Text);
iSum:=0;
for i := 0 to iCnt do
begin
iSum:=iSum+SumTotal(i);
end;
Memo1.Lines.Add(IntToStr(iCnt)+' : '+IntToStr(iSum));
end;
function TForm1.SumTotal(ACnt: Integer): Integer;
var
i:Integer;
begin
Result:=0;
for i := 0 to ACnt do
begin
Result:=Result+i;
end;
end;
end.
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Memo1: TMemo;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
function SumTotal(ACnt:Integer):Integer;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
var
i,iCnt,iSum:Integer;
begin
iCnt:=StrToInt(Edit1.Text);
iSum:=0;
for i := 0 to iCnt do
begin
iSum:=iSum+SumTotal(i);
end;
Memo1.Lines.Add(IntToStr(iCnt)+' : '+IntToStr(iSum));
end;
function TForm1.SumTotal(ACnt: Integer): Integer;
var
i:Integer;
begin
Result:=0;
for i := 0 to ACnt do
begin
Result:=Result+i;
end;
end;
end.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
var i,s,item:integer;
begin
s:=0;
item:=0;
for i := 1 to 100 do
begin
item:=item+i;
s:=s+item;
end;
showmessage('s='+inttostr(s));
end;
begin
s:=0;
item:=0;
for i := 1 to 100 do
begin
item:=item+i;
s:=s+item;
end;
showmessage('s='+inttostr(s));
end;
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
楼上说的应该没问题,为什麼不结案呢?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询