• 主页
• 问答

# 求解小学奥数算法

5-(3*4/6+2)
4*6/(5+3)-2
-------------------------------------
(最好是C或PASCAL的源程序)
...全文
17 点赞 收藏 9

9 条回复

ZhangYv说的对,光5!*4!是不够的

ZhangYv 2003-04-30

BlueSky2008 2003-04-30

mathe 2003-04-30
5!*4!才2880，穷举

ZhangYv 2003-04-30

mathe 2003-04-30

mathe 2003-04-30
27组解（加法和乘法交换意义下相同的解已经淘汰。不过也一些解结果可能是-1

5/(4+3-6*2)
(4+3-6*2)/5
4/(5+3-6*2)
(5+3-6*2)/4
3/(6+5-4*2)
(6+5-4*2)/3
3/(5+4-6*2)
(5+4-6*2)/3
(5+4)/(3-6*2)
(3-6*2)/(5+4)
(5+3)/(4-6*2)
(4-6*2)/(5+3)
(4+3)/(5-6*2)
(5-6*2)/(4+3)
4*2/(6+5-3)
4*2/(5+6-3)
4*(5-3)/(6+2)
(5-3*(6+2)/4)
(4-(3+6*2)/5)
(3-(6+5*2)/4)
(3-2*(6+4)/5)
(2-6*4/(5+3))
(6+3-5*4/2)
(6+3-4*5/2)
(5+2-6*4/3)
(5+2-4*6/3)
(5+6/3-4*2)
Total 27 expressions

void tree_iterator::stat_op(int d[])const
{
int i;
int pi,mi;
if(first_child==NULL)return;
tree_iterator *child=first_child;
if(get_prod){
pi=2; // *
mi=3; // /
}else{
pi=0; // +
mi=1; // -
}
d[pi]--;
for(i=0;i<spliter.get_sub_group_count();++i)
{
if(pattern&(1<<i)){
d[pi]++;// *
}else{
d[mi]++;// /
}
child->stat_op(d);
child=child->next_sibling;
}
}

int main()
{
int a[5]={2,3,4,5,6};
int count=0;
int d[4];
int cur_scan=0;
{
tree_iterator it(5,a,1);
do
{
d[0]=d[1]=d[2]=d[3]=0;
it.stat_op(d);
if(d[0]!=1||d[1]!=1||d[2]!=1||d[3]!=1)
continue;
if(it.get_value().is_integer()){
int value=it.get_value().get_integer();
if(value==1){
cout<<it.expression()<<endl;
count++;
}
}
}while(it.inc());
}
{
tree_iterator it(5,a,0);
do
{
d[0]=d[1]=d[2]=d[3]=0;
it.stat_op(d);
if(d[0]!=1||d[1]!=1||d[2]!=1||d[3]!=1)
continue;
if(it.get_value().is_integer()){
int value=it.get_value().get_integer();
if(value==1){
cout<<it.expression()<<endl;
count++;
}
}
}while(it.inc());
}
cout << "Total " << count << string(" expressions") << endl;
return 0;
}

FishCrazy 2003-04-30

3.2w+