
05.06.2013, 17:56
|
Местный
|
|
Регистрация: 09.11.2010
Сообщения: 499
Репутация: 10
|
|
Как-то так
Код:
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo1.Lines.Clear();
GenString();
end;
// вычисление строки
function Calc100Sum(S:string):Integer;
var
Temp:Integer;
Op:Char;
I:Integer;
procedure CalcOp;
begin
if Op = '=' then Result:=Temp;
if Op = '-' then Result:=Result - Temp;
if Op = '+' then Result:=Result + Temp;
end;
begin
Result:=0;
Temp:=0;
Op:='=';
for I:= 1 to Length(S) do
begin
case S[i] of
'0'..'9': Temp:=Temp*10 + Ord(S[i])-Ord('0');
'+','-': begin CalcOp(); Temp:=0; Op:=S[i]; end;
end;
end;
CalcOp();
end;
// генерация всевозможных строк
procedure TForm1.GenString();
var
I1,I2,I3,I4,I5,I6,I7,I8:Integer;
S:string;
Sum:Integer;
const
Ops:array[0..2] of string=('','+','-');
begin
for I1:=0 to 2 do
for I2:=0 to 2 do
for I3:=0 to 2 do
for I4:=0 to 2 do
for I5:=0 to 2 do
for I6:=0 to 2 do
for I7:=0 to 2 do
for I8:=0 to 2 do
begin
S:='1'
+ Ops[I1] + '2'
+ Ops[I2] + '3'
+ Ops[I3] + '4'
+ Ops[I4] + '5'
+ Ops[I5] + '6'
+ Ops[I6] + '7'
+ Ops[I7] + '8'
+ Ops[I8] + '9';
Sum := Calc100Sum(S);
if Sum = 100 then
Memo1.Lines.Add(S+' = 100');
end;
end;
|