请教一个24点算法的问题(200分)

  • 主题发起人 主题发起人 liugaohui
  • 开始时间 开始时间
L

liugaohui

Unregistered / Unconfirmed
GUEST, unregistred user!
以下是CSDN的版主starfish(海星)的算法,能否翻译成delphi的,请各位高手帮忙,水平太低,看不懂

算法:任取两个数,两两进行运算,这样剩下3个数了;然后再任取两个数,两两进行运算,重复上面过程,……最后得到的结果如果是24则输出
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
const do
uble PRECISION = 1E-6;
const int COUNT_OF_NUMBER = 4;
const int NUMBER_TO_BE_CAL = 24;
class RationalNumber {
protected:
int numerator, denominator;
bool inf;
protected:
int gcd(int a, int b) {
if (b==0) return a;
else
return gcd(b, a % b);
}
public:
RationalNumber() { inf = false;
}
RationalNumber(int n) {
numerator = n;
denominator = 1;
inf = false;
}
RationalNumber(int numerator, int denominator) {
this->numerator = numerator;
this->denominator = denominator;
Simplify();

}

virtual ~RationalNumber() {}
void Simplify() {
if (denominator == 0) {
inf = true;
} else
if (numerator == 0) {
denominator = 1;
inf = false;
} else
{
int k = gcd(abs(numerator), abs(denominator));
numerator /= k;
denominator /= k;

inf = false;
}
}
RationalNumber operator+(const RationalNumber& b) const {
RationalNumber result;
result.denominator = this->denominator * b.denominator;
result.numerator = this->numerator * b.denominator + this->denominator * b.numerator;
result.Simplify();

return result;
}

RationalNumber operator-(const RationalNumber& b) const {
RationalNumber result;
result.denominator = this->denominator * b.denominator;
result.numerator = this->numerator * b.denominator - this->denominator * b.numerator;
result.Simplify();

return result;
}
RationalNumber operator*(const RationalNumber& b) const {
RationalNumber result;
result.denominator = this->denominator * b.denominator;
result.numerator = this->numerator * b.numerator;
result.Simplify();

return result;
}
RationalNumber operator/(const RationalNumber& b) const {
RationalNumber result;
result.denominator = this->denominator * b.numerator;
result.numerator = this->numerator * b.denominator;
result.Simplify();

return result;
}
RationalNumber& operator=(const RationalNumber& b) {
denominator = b.denominator;
numerator = b.numerator;
return (*this);
}
RationalNumber& operator=(int b) {
denominator = 1;
numerator = b;
return (*this);
}
bool operator==(const RationalNumber& b) const {
return ( (denominator == b.denominator) && (numerator == b.numerator) );
}
bool operator==(int b) const {
return ( (denominator == 1) && (numerator == b) );
}
bool operator!=(const RationalNumber& b) const {
return ( (denominator != b.denominator) || (numerator != b.numerator) );
}
bool operator!=(int b) const {
return ( (denominator != 1) || (numerator != b) );
}
int Numerator() const { return numerator;
}
int Denominator() const { return denominator;
}
};

RationalNumber number[COUNT_OF_NUMBER];
string expression[COUNT_OF_NUMBER];
bool Search(int n)
{
if (n == 1) {
if ( number[0] == NUMBER_TO_BE_CAL ) {
cout << expression[0] << endl;
return true;
} else
{
return false;
}
}
for (int i = 0;
i < n;
i++) {
for (int j = i + 1;
j < n;
j++) {
RationalNumber a, b;
string expa, expb;
a = number;
b = number[j];
number[j] = number[n - 1];
expa = expression;
expb = expression[j];
expression[j] = expression[n - 1];
expression = '(' + expa + '+' + expb + ')';
number = a + b;
if ( Search(n - 1) ) return true;

expression = '(' + expa + '-' + expb + ')';
number = a - b;
if ( Search(n - 1) ) return true;

expression = '(' + expb + '-' + expa + ')';
number = b - a;
if ( Search(n - 1) ) return true;

expression = '(' + expa + '*' + expb + ')';
number = a * b;
if ( Search(n - 1) ) return true;
if (b != 0) {
expression = '(' + expa + '/' + expb + ')';
number = a / b;
if ( Search(n - 1) ) return true;
}
if (a != 0) {
expression = '(' + expb + '/' + expa + ')';
number = b / a;
if ( Search(n - 1) ) return true;
}
number = a;
number[j] = b;
expression = expa;
expression[j] = expb;
}
}
return false;
}
void main()
{
for (int i = 0;
i < COUNT_OF_NUMBER;
i++) {
char buffer[20];
int x;
cin >> x;
number = x;
itoa(x, buffer, 10);
expression = buffer;
}
if ( Search(COUNT_OF_NUMBER) ) {
cout << &quot;Success.&quot;
<< endl;
} else
{
cout << &quot;Fail.&quot;
<< endl;
}
}
 
原帖见:http://topic.csdn.net/t/20020227/23/546223.html
 
我试试来翻译下,呵呵,请稍等。
 
贡献点资料,请参考
http://dev.csdn.net/develop/article/20/20064.shtm
 
谢谢两位朋友,顶一下
 
操作符重载不太好写,回头用2006试试吧,呵呵,吃饭去了。
 
请各位看看这个对不对?
来源:http://www.codefans.com/download.asp?CodeID=8791
{
某份技巧集看到的算法,我随便写了写,
我不是作者但是我有不知道谁是作者,
所以写完收工。
sxbug@163.com
}
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,strutils;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
function SearchExpression(mNumbers: array of Integer;
mDest: Integer): string;
const
cPrecision = 1E-6;
var
vNumbers: array of Extended;
vExpressions: array of string;
vLength: Integer;
function fSearchExpression(mLevel: Integer): Boolean;
var
I, J: Integer;
A, B: Extended;
vExpA, vExpB: string;
begin
Result := True;
if (mLevel <= 1) and (Abs(vNumbers[0] - mDest) <= cPrecision) then
Exit;
for I := 0 to mLevel - 1do
begin
for J := I + 1 to mLevel - 1do
begin
A := vNumbers;
B := vNumbers[J];
vNumbers[J] := vNumbers[mLevel - 1];
vExpA := vExpressions;
vExpB := vExpressions[J];
vExpressions[J] := vExpressions[mLevel - 1];
vExpressions := '(' + vExpA + '+' + vExpB + ')';
vNumbers := A + B;
if fSearchExpression(mLevel - 1) then
Exit;
vExpressions := '(' + vExpA + '-' + vExpB + ')';
vNumbers := A - B;
if fSearchExpression(mLevel - 1) then
Exit;
vExpressions := '(' + vExpB + '-' + vExpA + ')';
vNumbers := B - A;
if fSearchExpression(mLevel - 1) then
Exit;
vExpressions := '(' + vExpA + '*' + vExpB + ')';
vNumbers := A * B;
if fSearchExpression(mLevel - 1) then
Exit;
if B <> 0 then
begin
vExpressions := '(' + vExpA + '/' + vExpB + ')';
vNumbers := A / B;
if fSearchExpression(mLevel - 1) then
Exit;
end;
if A <> 0 then
begin
vExpressions := '(' + vExpB + '/' + vExpA + ')';
vNumbers := B / A;
if fSearchExpression(mLevel - 1) then
Exit;
end;
vNumbers := A;
vNumbers[J] := B;
vExpressions := vExpA;
vExpressions[J] := vExpB;
end;
end;
Result := False;
end;
var
I: Integer;
begin
vLength := Length(mNumbers);
SetLength(vNumbers, vLength);
SetLength(vExpressions, vLength);
for I := 0 to vLength - 1do
begin
vNumbers := mNumbers;
vExpressions := IntToStr(mNumbers);
end;
if fSearchExpression(vLength) then
Result := vExpressions[0]
else
Result := '';
vNumbers := nil;
vExpressions := nil;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
a1,a2,a3,a4,d1:Integer;
str:String;
begin
if POS(',',Edit1.Text )=0 then
begin
ShowMessage('输入的格式不对,请检查');
Exit;
end;
Str:=Edit1.Text;
try
a1:=strtoint(Trim(leftstr(Str,Pos(',', Str)-1)));
except
ShowMessage('输入的格式不对,请检查');
exit;
end;
Str:=rightStr(Str,length(Str)-Pos(',', Str));
try
a2:=strtoint(Trim(leftstr(Str,Pos(',', Str)-1)));
except
ShowMessage('输入的格式不对,请检查');
exit;
end;
Str:=rightStr(Str,length(Str)-Pos(',', Str));
try
a3:=strtoint(Trim(leftstr(Str,Pos(',', Str)-1)));
except
ShowMessage('输入的格式不对,请检查');
exit;
end;
Str:=rightStr(Str,length(Str)-Pos(',', Str));
try
a4:=strtoint(Trim(leftstr(Str,Pos(',', Str)-1)));
except
ShowMessage('输入的格式不对,请检查');
exit;
end;
Str:=rightStr(Str,length(Str)-Pos(',', Str));
try
D1:=strtoInt(Trim(Str));
except
ShowMessage('输入的格式不对,请检查');
exit;
end;
Edit1.Text := SearchExpression([a1, a2, a3, a4], d1);
end;

procedure TForm1.FormShow(Sender: TObject);
begin
edit1.Text:='5, 5, 5, 1, 24';
end;

end.
 
应该对吧,我这两天忙项目去了,还没时间翻译。不过您的代码试试就知道对不对了。呵呵。
 
后退
顶部