标题:给定四个数字, 生成四个数字能构成的所有表达式树
只看楼主
Raylastin
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2019-6-23
结帖率:50%
已结贴  问题点数:10 回复次数:1 
给定四个数字, 生成四个数字能构成的所有表达式树
一个模糊的思路如下:

    void gen_expression_tree(BiTree &t, int num)

    {

     for(int i = 1; i < num; i++) {      

         t->data = “+-*/”中的一者 //按次序枚举
     
         gen_expression_tree(t->lchild, i);

         gen_expression_tree(t->rchild, num - i);
     }

  }
  num是树中的数字个数。当num=1时,为叶子结点,结点的data域应该是数字,数字的出现次序也是有序的。
  sample Output部分给出了输出的最后部分:
(1+(2+(3+4)))
(1+(2+(4+3)))
(1+(2+(3-4)))
(1+(2+(4-3)))
(1+(2+(3*4)))
(1+(2+(4*3)))
(1+(2+(3/4)))
(1+(2+(4/3)))
(1+(3+(2+4)))
(1+(3+(4+2)))
(1+(3+(2-4)))
(1+(3+(4-2)))
......
搜索更多相关主题的帖子: 给定 数字 生成 表达式 num 
2019-06-25 09:10
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:507
帖 子:8890
专家分:53117
注 册:2011-1-18
得分:10 
四个数,加减乘除,共有1170个不同的表达式。
以下是这1170个逆波兰表达式
ab*c*d*
ab*c*d+
ab*c*d-
ab*c*d/
ab*c+d*
ab*c+d+
ab*c+d-
ab*c+d/
ab*c-d*
ab*c-d+
ab*c-d-
ab*c-d/
ab*c/d+
ab*c/d-
ab*c/d/
ab*cd*+
ab*cd*-
ab*cd+*
ab*cd+/
ab*cd-*
ab*cd-/
ab*cd/+
ab*cd/-
ab*d*c+
ab*d*c-
ab*d*c/
ab*d+c*
ab*d+c/
ab*d-c*
ab*d-c/
ab*d/c+
ab*d/c-
ab*dc-*
ab*dc-/
ab*dc/+
ab*dc/-
ab+c*d*
ab+c*d+
ab+c*d-
ab+c*d/
ab+c+d*
ab+c+d+
ab+c+d-
ab+c+d/
ab+c-d*
ab+c-d+
ab+c-d-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cda/+b-
cda/+b/
cda/-b/
cda/b-/
cda/b/+
cda/b/-
cdab*-/
cdab+/+
cdab+/-
cdab-/+
cdab-/-
cdab/-/
cdb-/a-
cdb/+a-
cdb/+a/
cdb/-a/
cdb/a-/
cdba/-/
da-b*c*
da-b*c-
da-b*c/
da-b-c*
da-b-c-
da-b-c/
da-b/c-
da-b/c/
da-bc*-
da-bc+*
da-bc+/
da-bc-*
da-bc-/
da-bc/-
da-c*b-
da-c*b/
da-c-b*
da-c-b/
da-c/b-
da-cb/-
da/b-c*
da/b-c-
da/b-c/
da/b/c-
da/b/c/
da/bc*-
da/bc+/
da/bc-/
da/bc/-
da/c-b/
da/c/b-
da/cb-/
da/cb/-
dab*-c*
dab*-c-
dab*-c/
dab*c*-
dab*c+/
dab*c-/
dab*c/-
dab+/c-
dab+/c/
dab+c*-
dab+c+/
dab+c-/
dab+c/-
dab-/c-
dab-/c/
dab-c*-
dab-c+/
dab-c-/
dab-c/-
dab/-c*
dab/-c-
dab/-c/
dab/c+/
dab/c-/
dab/c/-
dabc*+/
dabc*-/
dabc+*-
dabc+/-
dabc-*-
dabc-/-
dabc/+/
dabc/-/
dac*-b*
dac*-b-
dac*-b/
dac*b+/
dac*b-/
dac*b/-
dac+/b-
dac+/b/
dac+b*-
dac+b/-
dac-/b-
dac-/b/
dac-b*-
dac-b/-
dac/-b*
dac/-b-
dac/-b/
dac/b+/
dac/b-/
dacb/+/
dacb/-/
db-a/c-
db-a/c/
db-ac+/
db-ac-/
db-c*a-
db-c*a/
db-c-a/
db-c/a-
db-ca/-
db/a-c*
db/a-c-
db/a-c/
db/ac*-
db/ac/-
db/c-a/
db/c/a-
db/ca/-
dba-/c-
dba-/c/
dba-c-/
dba/-c*
dba/-c-
dba/-c/
dba/c+/
dba/c-/
dba/c/-
dbac*-/
dbac+/-
dbac-/-
dbac/-/
dbc*-a/
dbc*a-/
dbc*a/-
dbc+/a-
dbc+a-/
dbc+a/-
dbc-/a-
dbc-a/-
dbc/-a/
dbc/a-/
dbca/+/
dbca/-/
dc-a/b-
dc-a/b/
dc-ab+/
dc-ab-/
dc-b/a-
dc/a-b*
dc/a-b-
dc/a-b/
dc/ab*-
dc/ab/-
dc/b-a/
dc/ba/-
dca-/b-
dca-/b/
dca-b-/
dca/-b/
dca/b-/
dca/b/-
dcab*-/
dcab+/-
dcab-/-
dcab/-/
dcb-/a-
dcb/-a/
dcb/a-/
dcba/-/

2019-07-02 13:05



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-495492-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.048786 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved