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 << "Success."
<< endl;
} else
{
cout << "Fail."
<< endl;
}
}
算法:任取两个数,两两进行运算,这样剩下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 << "Success."
<< endl;
} else
{
cout << "Fail."
<< endl;
}
}