计蒜客 结果填空:加减乘

题目链接:点击打开链接

请对于下面式子进行填空,填入加减乘,使这个表达式成立。

11  22  33  44  55  66  77  88  99  1010 == 00

请输出一共有多少种方案可以使得表达式成立

一开始忽略了有乘和加应该先算乘号。每算一步之前应该考虑先一步的符号,若是加减之间计算,否则先计算后面一步。

#include<stdio.h>
int f[10]={1,2,3,4,5,6,7,8,9,10};
int s;
int deal(int a,char c,int b)
{
if(c=='+')
return a+b;
else
return a-b;
}
void dfs(int a,char c,int b,int step)
{
if(step==10)
{
if(deal(a,c,b)==0)
s++;
return ;
}
dfs(deal(a,c,b),'+',f[step],step+1);
dfs(deal(a,c,b),'-',f[step],step+1);
dfs(a,c,(b*f[step]),step+1);
}
int main()
{
s=0;
dfs(0,'+',1,1);
printf("%d\n",s);
return 0;
}

猜你喜欢

转载自blog.csdn.net/z2664836046/article/details/79429935
今日推荐