免费预览已结束,剩余6页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
24点算法的Java版和C语言版源码Java版共2个类:Application.java 和 Node.java= Application.java=package num24;public class Application double num = new double4; boolean mark = new boolean4; /* * 获得k个数运算,结果为result的第一个Node,比如get(24,4) * param result * param n * return */ public Node get(double result, int n) Node left = null, right = null; if (n = 0) return null; /如果个数小于0,返回空 for (int i = 0; i 1e-10) left = get(result / numi, n - 1); if (left != null) return new Node(0.0, *, left, new Node(numi); for (int j = 0; j 1e-10) if (result = numi / numj & n = 2) return new Node(0.0, /, new Node(numi), new Node(numj); left = get(result + numi / numj, n - 2); /a/b-(c?d)=result=c?d=result+a/b if (left != null) right = new Node(0.0, /, new Node(numi), new Node(numj); return new Node(0.0, -, left, right); left = get(result - numi / numj, n - 2); if (left != null) right = new Node(0.0, /, new Node(numi), new Node(numj); return new Node(0.0, +, left, right); if(result=numi-numj&n=2) return new Node(0.0,-,new Node(numi),new Node(numj); if(Math.abs(numi-numj)1e-10) left=get(result/(numi-numj),n-2); if(left!=null) right=new Node(0.0,-,new Node(numi),new Node(numj); return new Node(0.0,*,left,right); markj=false; /end for(int j. marki=false; /end for(int i. return null; public void output(Node node) if (node = null) return; if (node.left != null) if ( (.equals(+) | .equals(-) & (.equals(*) | .equals(/) System.out.print(); output(node.left); System.out.print(); else output(node.left); if (!.trim().equals() System.out.print(); else System.out.print(node.num); if (node.right != null) if ( (.equals(+) | .equals(-) & (.equals(*) | .equals(/) | (.equals(+) | .equals(-) & (.equals(-) | (!.trim().equals() & (.equals(/) ) System.out.print(); output(node.right); System.out.print(); else output(node.right); /* * * param args */ public static void main(String args) Application app=new Application(); app.num0=Double.parseDouble(args0); app.num1=Double.parseDouble(args1); app.num2=Double.parseDouble(args2); app.num3=Double.parseDouble(args3); double result; Node tree=null; /while(true) result=24.0; / if(result=-1.0)break; app.mark0=false; app.mark1=false; app.mark2=false; app.mark3=false; tree=app.get(result,4); if(tree!=null) app.output(tree); System.out.println(=+result); else System.out.println(No!); / =Node.java=package num24;public class Node public double num; String name; Node left, right = null; public Node() num = 0.0; name = ; left = right = null; public Node(double aNum) this(aNum, ,null,null); public Node(double aNum, String aName, Node aLeft, Node aRight) num = aNum; name = aName; left = aLeft; right = aRight; public void delete() if(left!=null) left.delete(); if(right!=null) right.delete(); left=null; right=null; C 语言版/用二叉树保存表达式,输出无多余括号。#include#include#includeusing namespace std;double n4;bool mark4;class nodepublic:double num;char c;node *left,*right;node()num=0.0;c= ;left=right=NULL;node(double x,char ch= ,node *l=NULL,node *r=NULL)num=x;c=ch;left=l;right=r;void del()if(left!=NULL)left-del();if(right!=NULL)right-del();delete(left);delete(right);node()del();/获得k个数运算,结果为r的node/get(24,4)node *get(double r,int k)int i,j;node *lc,*rc;if(k=0)return NULL;for(i=0;i1e-10)lc=get(r/ni,k-1);if(lc!=NULL)return new node(0.0,*,lc,new node(ni);for(j=0;j1e-10)if(r=ni/nj&k=2)return new node(0.0,/,new node(ni),new node(nj);lc=get(r+ni/nj,k-2);if(lc!=NULL)rc=new node(0.0,/,new node(ni),new node(nj);return new node(0.0,-,lc,rc);lc=get(r-ni/nj,k-2);if(lc!=NULL)rc=new node(0.0,/,new node(ni),new node(nj);return new node(0.0,+,lc,rc);if(r=ni+nj&k=2)return new node(0.0,+,new node(ni),new node(nj);if(fabs(ni+nj)1e-10)lc=get(r/(ni+nj),k-2);if(lc!=NULL)rc=new node(0.0,+,new node(ni),new node(nj);return new node(0.0,*,lc,rc);if(r=ni-nj&k=2)return new node(0.0,-,new node(ni),new node(nj);if(fabs(ni-nj)1e-10)lc=get(r/(ni-nj),k-2);if(lc!=NULL)rc=new node(0.0,-,new node(ni),new node(nj);return new node(0.0,*,lc,rc);markj=false;marki=false;return NULL;void output(node* T)if(T=NULL)return;if(T-left!=NULL)if(T-left-c=+|T-left-c=-)&(T-c=*|T-c=/)coutleft);coutleft);if(T-c!= )coutc;elsecoutnum;if(T-right!=NULL)if(T-right-c=+|T-right-c=-)&(T-c=*|T-c=/)|(T-right-c=+|T-right-c=-)&(T-c=-)|(T-right-c!= )&(T-c=/) )coutright);coutright);int main()double result;node*
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年劳资专管员培训考试题库及答案
- 2025办公室工作年终总结范文3篇
- 2025年中远海运国际分公司招聘面试题与答题思路解析
- 2025商业房地产买卖合同
- 2025年下半年吉林省长春市公用事业信息中心工作人员招聘2人易考易错模拟试题(共500题)试卷后附参考答案
- 2025年下半年台州市民政局下属事业单位招考易考易错模拟试题(共500题)试卷后附参考答案
- 2025年下半年南通市通州区招考内河交通安全管理辅助执法人员易考易错模拟试题(共500题)试卷后附参考答案
- 2025年下半年南昌市青山湖区专职招商人员招考(10人)易考易错模拟试题(共500题)试卷后附参考答案
- 2025年下半年南京军区房地产管理局南平分局招考易考易错模拟试题(共500题)试卷后附参考答案
- 2025年下半年北京市怀柔区面向社会招聘事业单位工作人员笔试易考易错模拟试题(共500题)试卷后附参考答案
- 初中英语固定搭配及词组
- 绵阳介绍-简单英语
- 集体备课培训材料课件
- 实验室危险源、风险点重点排查项目表
- 供应链可持续发展与企业社会责任管理
- 卫生部修订病历书写基本规范(全文)
- 体育中国2023章节测试答案-体育中国超星尔雅答案
- GB/T 8642-2002热喷涂抗拉结合强度的测定
- 辽宁盘锦浩业化工“1.15”泄漏爆炸着火事故警示教育
- 式叉摩那尼戒本
- 安徽师范大学2023年616马克思主义基本原理考研真题(回忆版)
评论
0/150
提交评论