欢迎来到天天文库
浏览记录
ID:20700094
大小:132.55 KB
页数:14页
时间:2018-10-15
《回溯法的24点问题》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、#include#includeusingnamespacestd;#includeconstdoublePRECISION=IE-6;//精度常量constintCOUNT_OF_NUMBER=4;//算24点的然数个数constintNUMBERTOBECAL=24;boolflag=false;classRationalNumber//定义有理数类(分子、分母){protected:intnumerator,denominator;//numera
2、tor:分子,denominator:分母boolinf;//整数假protected:intgcd(inta,intb)//求a和b的最大公约数{inttemp;if(a
3、intnumerator,intdenominator)this->numerator=numerator;this->denominator=denominator;SimplifyO;}virtual〜RationalNuniber(){}voidSimplifyO{if(denominator==1){inf=false;}elseif(numerator==0){denominator:1;inf=false;}else{intk=gcd(abs(numerator),abs(denominato
4、r));numerator/=k;denominator/=k;if(denominator==1)inf=false;elseinf=true;}}RationalNumberoperator+(constRationalNumber&b)const{RationalNumberresult;result.denominator=this->denominator*b.denominator;result.numerator=this->numerator*b.denominator+this->den
5、ominator*b.numerator;result.SimplifyO;returnresult;)RationalNumberoperator-(constRationalNumber&b)const{//ooooooRationalNumberresult;result.denominator=this->denominator*b.denominator;result.numerator=this->numerator*b.denominator-this->denominator*b.nume
6、rator;result.SimplifyO;returnresult;}RationalNumberoperator*(constRationalNumber&b)constf//ooooooRationalNumberresult;result.denominatoi-this->denominator*b.denominator;result.numerator=this->numerator*b.numerator;result.Simplify();returnresult;}RationalN
7、umberoperator/(constRationalNumber&b)const{RationalNumberresult;result.denominatoi-this->denominator*b.numerator;result.numerator=this->numerator*b.denominator;result.SimplifyO;returnresult;)RationalNumber&operator=(constRationalNumber&b){denominator=b.de
8、nominator;numerator=b.numerator;return(*this);}RationalNumber&operator=(intb){denominator=l;numerator=b;return(*this);}intNumerator()const{returnnumerator;}intDenominator()const{returndenominator;}stringstrshow(){st
此文档下载收益归作者所有