我这里用a代表单数b代表双数 先声明两个全局变量: array1: array [0..4] of integer; array2: array [0..4] of integer; procedure TForm1.Button1Click(Sender: TObject); var i,j:integer; begin memo1.Clear; for i:=0 to 4 do begin if LeftStr(Edit1.Text,1)='a' then array1[i]:=i*2+1 else if LeftStr(Edit1.Text,1)='b' then array1[i]:=i*2; if RightStr(Edit1.Text,1)='a' then array2[i]:=i*2+1 else if RightStr(Edit1.Text,1)='b' then array2[i]:=i*2; end; for i:=0 to 4 do begin for j:=0 to 4 do begin Memo1.Lines.Add(inttostr(array1[i])+copy(edit1.Text,2,2)+inttostr(array2[j])); end; end; end;
呵呵,比那个什么小学奥林匹克的题目还难理解。 不过,题目倒是很简单啊: //得到某一位可取的值,反正都是一位,就用个串来存就是了,懒得用数组了。 function GetCharIndexValue(const c: string; const Index: integer): String; begin if CompareText(c, '单') then Result := '13579' else if CompareText(c, '双') then Result := '02468' else Result := c; end; 类似“单01双”的串建议定义成WideString,不要用全角符号,懒得判断。
var i, j, k, l: integer; s1, s2, s3, s4: string; begin //调用时,先分别得到四个数字的可取值 ss存“单01双”之类,WideString s1 := GetCharIndexValue(ss[1], 1); s2 := GetCharIndexValue(ss[2], 2); s3 := GetCharIndexValue(ss[3], 3); s4 := GetCharIndexValue(ss[4], 4);一个嵌套循环就可以了: for i := 1 to Length(s1) do for j := 1 to Length(s2) do for k := 1 to Length(s3) do for l := 1 to Length(s4) do AddStr(s1 + s2 + s3 + s4);
现在可以了。用ssq237712的 程序。如下: function GetCharIndexValue(const c: string; const Index: integer): String; begin if c='单' then begin Result := '13579'; exit; end; if c='双' then begin Result := '02468'; exit; end; if (c='X') or (c='') then begin result:='0123456789'; exit; end; Result :=c; end; procedure getNum(stringGrid:TStringGrid;Num1,num2,num3,num4:string); var s1,s2,s3,s4:string; i,j,k,l:integer; n,icol,irow:integer; begin cleargride(stringgrid); N:=0; iCol:=0; iRow:=0; s1:=GetCharIndexValue(num1,1); s2:=GetCharIndexValue(num2,2); s3:=GetCharIndexValue(num3,3); s4:=GetCharIndexValue(num4,4); for i := 1 to Length(s1) do for j := 1 to Length(s2) do for k := 1 to Length(s3) do for l := 1 to Length(s4) do begin if iCol=12 then begin iCol:=0; iRow:=iRow+1; end; stringGrid.Cells[icol,iRow]:=copy(s1,i,1)+copy(s2,j,1)+copy(s3,k,1)+copy(s4,l,1); N:=N+1; iCol:=iCol+1; end; end;
先声明两个全局变量:
array1: array [0..4] of integer;
array2: array [0..4] of integer;
procedure TForm1.Button1Click(Sender: TObject);
var i,j:integer;
begin
memo1.Clear;
for i:=0 to 4 do
begin
if LeftStr(Edit1.Text,1)='a' then array1[i]:=i*2+1
else if LeftStr(Edit1.Text,1)='b' then array1[i]:=i*2;
if RightStr(Edit1.Text,1)='a' then array2[i]:=i*2+1
else if RightStr(Edit1.Text,1)='b' then array2[i]:=i*2;
end;
for i:=0 to 4 do
begin
for j:=0 to 4 do
begin
Memo1.Lines.Add(inttostr(array1[i])+copy(edit1.Text,2,2)+inttostr(array2[j]));
end;
end;
end;
不过,题目倒是很简单啊:
//得到某一位可取的值,反正都是一位,就用个串来存就是了,懒得用数组了。
function GetCharIndexValue(const c: string; const Index: integer): String;
begin
if CompareText(c, '单') then
Result := '13579'
else if CompareText(c, '双') then
Result := '02468'
else
Result := c;
end; 类似“单01双”的串建议定义成WideString,不要用全角符号,懒得判断。
var
i, j, k, l: integer;
s1, s2, s3, s4: string;
begin
//调用时,先分别得到四个数字的可取值 ss存“单01双”之类,WideString
s1 := GetCharIndexValue(ss[1], 1);
s2 := GetCharIndexValue(ss[2], 2);
s3 := GetCharIndexValue(ss[3], 3);
s4 := GetCharIndexValue(ss[4], 4);一个嵌套循环就可以了:
for i := 1 to Length(s1) do
for j := 1 to Length(s2) do
for k := 1 to Length(s3) do
for l := 1 to Length(s4) do
AddStr(s1 + s2 + s3 + s4);
AddStr(s1[i] + s2[j] + s3[k] + s4[l]);
我开始已经说明是四个EDIT中输入,而非一个EDIT。再者hellolongbin(一个人) 的程序有点我只是举个例子,你这样写也太不灵活了吧。
谢谢大家了,看看思路,我试试。散分!多谢大家帮忙!
function GetCharIndexValue(const c: string; const Index: integer): String;
begin
if c='单' then
begin
Result := '13579';
exit;
end;
if c='双' then
begin
Result := '02468';
exit;
end;
if (c='X') or (c='') then
begin
result:='0123456789';
exit;
end;
Result :=c;
end;
procedure getNum(stringGrid:TStringGrid;Num1,num2,num3,num4:string);
var
s1,s2,s3,s4:string;
i,j,k,l:integer;
n,icol,irow:integer;
begin
cleargride(stringgrid);
N:=0;
iCol:=0;
iRow:=0;
s1:=GetCharIndexValue(num1,1);
s2:=GetCharIndexValue(num2,2);
s3:=GetCharIndexValue(num3,3);
s4:=GetCharIndexValue(num4,4);
for i := 1 to Length(s1) do
for j := 1 to Length(s2) do
for k := 1 to Length(s3) do
for l := 1 to Length(s4) do
begin
if iCol=12 then
begin
iCol:=0;
iRow:=iRow+1;
end;
stringGrid.Cells[icol,iRow]:=copy(s1,i,1)+copy(s2,j,1)+copy(s3,k,1)+copy(s4,l,1);
N:=N+1;
iCol:=iCol+1;
end;
end;