求高效率的四则运算算法(100分)

C

coca

Unregistered / Unconfirmed
GUEST, unregistred user!
求高效率的四则运算算法,如 1,2,3,4 四个数字不重复的全部加减乘除结果。
 
+ - / * 排列组合?
 
1,2,3,4 四个数字的排列组合和 "+ - / *" 排列组合,
1,2,3,4 四个数字在每个组合里不能重复(如: 1,2,3,4;2,1,3,4)
"+ - * / " 的组合必须是全部组合(包括重复和不重复,如+++, +*/,++-)
求高效率、不重复的全部四则运算结果算法。
如:
1+2+3+4=
1+2+3-4=
....
4+2*3/1=

 
to coca:
只能运算三次吧,怎么写了四个运算符。
如果等的急,就等我一会儿,现在没有开发工具。
 
既然要求全部的结果,也就没什么减少搜索空间的好方法了.
但我先把P4取4和C4取3算出来了,省得对每组数据都生成一遍.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TByteSet = set of Byte;
TArr4 = array [1..4] of Integer;
TArr3 = array [1..3] of Integer;
TForm1 = class(TForm)
ListBox1: TListBox;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
P44: array of TArr4;
C43: array of TArr3;
Temp: TArr4;
procedure MakeP44(Index: Integer;
Temp: TArr4;
Used: TByteSet);
procedure MakeC43;
public
{ Public declarations }
procedure Print(Num: TArr4;
Op: TArr3);
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.MakeC43;
var
i, j, k: Integer;
Count: Integer;
begin
SetLength(C43, 4*4*4);
Count:=-1;
for i:=1 to 4do
for j:=1 to 4do
for k:=1 to 4do
begin
Inc(Count);
C43[Count, 1]:=i;
C43[Count, 2]:=j;
C43[Count, 3]:=k
end
end;

procedure TForm1.MakeP44(Index: Integer;
Temp: TArr4;
Used: TByteSet);
var
d: Integer;
begin
for d:=1 to 4do
if not (d in Used) then
begin
Temp[Index]:=d;
if Index=4 then
begin
SetLength(P44, Length(P44)+1);
P44[Length(P44)-1]:=Temp
end
else
MakeP44(Index+1, Temp, Used+[d])
end
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
MakeC43;
MakeP44(1, Temp, [])
end;

procedure TForm1.Button1Click(Sender: TObject);
var
i, j: Integer;
begin
ListBox1.Items.Clear;
Temp[1]:=StrToInt(Edit1.Text);
Temp[2]:=StrToInt(Edit2.Text);
Temp[3]:=StrToInt(Edit3.Text);
Temp[4]:=StrToInt(Edit4.Text);
for i:=0 to Length(P44)-1do
for j:=0 to Length(C43)-1do
Print(P44, C43[j])
end;

procedure TForm1.Print(Num: TArr4;
Op: TArr3);
var
s: string;
i: Integer;
Value:do
uble;
begin
Value:=Temp[Num[1]];
s:='('+IntToStr(Temp[Num[1]]);
for i:=1 to 3do
begin
case Op of
1:
begin
s:=s+'+';
Value:=Value+Temp[Num[i+1]]
end;
2:
begin
s:=s+'-';
Value:=Value-Temp[Num[i+1]]
end;
3:
begin
s:=s+'*';
Value:=Value*Temp[Num[i+1]]
end;
4:
begin
s:=s+'/';
Value:=Value/Temp[Num[i+1]]
end
end;
s:=s+IntToStr(Temp[Num[i+1]])+')'
end;
s:=s+'='+FloatToStr(Value);
ListBox1.Items.Append(s)
end;

end.
 
function getNum(str:string):string;
var
stra,strb,F:string;
ipos1,ipos2,ipos3,ipos4:integer;
valuea,valueb:real;
Function Getvalue(s:string):Boolean;
begin
result:=true;
if pos('+',s)>0 then
result:=false;
if pos('-',s)>0 then
result:=false;
if pos('*',s)>0 then
result:=false;
if pos('/',s)>0 then
result:=false;
end;

Function GetMyresult(av,bv:real;F:string):string;
begin
try
if f='+' then
result:=floattostr(av+bv)
else
if f='-' then
result:=floattostr(av-bv)
else
if f='*' then
result:=floattostr(av*bv)
else
if (f='/') then
begin
if bv<>0 then
result:=floattostr(av/bv)
else
begin
Result:='ERROR';
exit;
end;
end
else
result:='ERROR';
except
result:='ERROR';
end;
end;
begin
try
ipos1:=pos('+',str);
ipos2:=pos('-',str);
ipos3:=pos('*',str);
ipos4:=pos('/',str);
if ipos1>0 then
begin
stra:=leftstr(str,ipos1-1);
strb:=rightstr(str,length(str)-ipos1);
f:='+';
end
else
if ipos2>0 then
begin
stra:=leftstr(str,ipos2-1);
strb:=rightstr(str,length(str)-ipos2);
f:='-';
end
else
if ipos3>0 then
begin
stra:=leftstr(str,ipos3-1);
strb:=rightstr(str,length(str)-ipos3);
f:='*';
end
else
if ipos4>0 then
begin
stra:=leftstr(str,ipos4-1);
strb:=rightstr(str,length(str)-ipos4);
f:='/';
end
else
begin
result:=trim(str);
exit;
end;

if Getvalue(stra) then
valuea:=strtofloat(stra)
else
valuea:=strtofloat(getnum(stra));
if Getvalue(strb) then
valueb:=strtofloat(strb)
else
valueb:=strtofloat(getnum(strb));
result:=GetMyresult(valuea,valueb,f);
except
result:='ERROR';
end;
end;

 
To LeeChange, 多谢!但你的方法没办法求出等于 24.0000 的 3,8,8,3
与 +-*/ 的组合。[:)]
To li_cj: 辛苦啦!
 
顶部