第1个回答 2011-08-12
Delphi版本代码.测试可以达到效果!
function TForm1.topic1(plural, minV, maxV: Integer): string;
var
iLoop:Integer;
begin
Result:='';
for iLoop:=minv to maxv do
begin
if iLoop mod plural >0 then
begin
Result:=Result+ IntToStr(iLoop)+',';
end;
end;
end;
function TForm1.topic2(plural: Integer): Integer;
var
iLoop:Integer;
begin
result:=0;
for iLoop:=1 to plural do
begin
if iLoop mod 2 =0 then
begin
Result:=Result+iLoop;
end;
end;
end;
function TForm1.topic3(): string;
var
randomArray:array [0..7] of Integer;
iLoop,jLoop:Integer;
buf:Integer;
begin
for iLoop:=0 to 7 do
begin
Randomize;
randomArray[iLoop]:= Random(100);
end;
// 冒泡法排序
for jLoop:=1 to 7-1 do
begin
for iLoop:=0 to 7-jLoop do
begin
if randomArray[iLoop]>randomArray[iLoop+1] then
begin
buf:=randomArray[iLoop];
randomArray[iLoop]:=randomArray[iLoop+1];
randomArray[iLoop+1]:=buf;
end;
end;
end;
result:='';
//输出结果
for iLoop:=0 to 7 do
begin
Result:=Result+ IntToStr(randomArray[iLoop])+',';
end;
end;
procedure TForm1.topic4;
const
xPlural=14;
yPlural=14;
LoopCount=9;
var
x,y:Integer;
i,j:Integer;
xbuf,ybuf:Integer;
begin
y:=50;
ybuf:=LoopCount*yPlural+50;
for i:=1 to LoopCount do
begin
x:=50;
if i mod 2=1 then
begin
x:=x+(LoopCount-i)*round(xPlural/2);
y:=y+yPlural;
xbuf:=x;
for j:=1 to i do
begin
Self.Canvas.TextOut(x,y,'*');
x:=x+xPlural;
end;
for j:=1 to i do
begin
Self.Canvas.TextOut(xbuf,ybuf,'*');
xbuf:=xbuf+xPlural;
end;
ybuf:=ybuf-yPlural;
end;
end;
end;
procedure TForm1.btn_TestClick(Sender: TObject);
begin
ShowMessage(topic1(3,100,200));
ShowMessage(IntToStr(topic2(100)));
ShowMessage(topic3());
topic4();
end;