社区
数据结构与算法
帖子详情
24点的程序如何实现?
Arbow
2002-02-27 11:11:22
设计一个程序,可以自动给出4个数,经过加减乘除和括号计算后等于24,我在某些电子辞典上看到有这种程序,电脑上还没见过。它的算法是怎样的?那位有原程序?
给出的给50分,不过我不懂如何给分,要告诉我啊
...全文
103
2
打赏
收藏
24点的程序如何实现?
设计一个程序,可以自动给出4个数,经过加减乘除和括号计算后等于24,我在某些电子辞典上看到有这种程序,电脑上还没见过。它的算法是怎样的?那位有原程序? 给出的给50分,不过我不懂如何给分,要告诉我啊
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
2 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
LLnju
2002-02-28
打赏
举报
回复
看看 http://www.csdn.net/expert/topic/503/503163.xml ,http://www.csdn.net/expert/topic/503/503180.xml 也许有用,你可以找 mathe 要源码,我的有点慢。还有要程序的时候记得留下 EMail
点管理、填分数、填密码、给分。
starfish
2002-02-28
打赏
举报
回复
算法:任取两个数,两两进行运算,这样剩下3个数了;然后再任取两个数,两两进行运算,重复上面过程,……最后得到的结果如果是24则输出
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
const double 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[i];
b = number[j];
number[j] = number[n - 1];
expa = expression[i];
expb = expression[j];
expression[j] = expression[n - 1];
expression[i] = '(' + expa + '+' + expb + ')';
number[i] = a + b;
if ( Search(n - 1) ) return true;
expression[i] = '(' + expa + '-' + expb + ')';
number[i] = a - b;
if ( Search(n - 1) ) return true;
expression[i] = '(' + expb + '-' + expa + ')';
number[i] = b - a;
if ( Search(n - 1) ) return true;
expression[i] = '(' + expa + '*' + expb + ')';
number[i] = a * b;
if ( Search(n - 1) ) return true;
if (b != 0) {
expression[i] = '(' + expa + '/' + expb + ')';
number[i] = a / b;
if ( Search(n - 1) ) return true;
}
if (a != 0) {
expression[i] = '(' + expb + '/' + expa + ')';
number[i] = b / a;
if ( Search(n - 1) ) return true;
}
number[i] = a;
number[j] = b;
expression[i] = 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[i] = x;
itoa(x, buffer, 10);
expression[i] = buffer;
}
if ( Search(COUNT_OF_NUMBER) ) {
cout << "Success." << endl;
} else {
cout << "Fail." << endl;
}
}
数据结构与算法
33,028
社区成员
35,337
社区内容
发帖
与我相关
我的任务
数据结构与算法
数据结构与算法相关内容讨论专区
复制链接
扫一扫
分享
社区描述
数据结构与算法相关内容讨论专区
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章