Problem : 等式
Description
有一个未完成的等式:1 2 3 4 5 6 7 8 9=N当给出整数N的具体值后,请你在2,3,4,5,6,7,8,9这
8个数字的每一个前面,或插入一个运算符号“+”号,或插入一个运算符号“-”号,或不插入任何运算符号,使
等式成立,并统计出能使等式成立的算式总数,若无解,则输出0。例如:取N为108时,共能写出15个不同的等式
,以下就是其中的二个算式:
1+23+4+56+7+8+9=108
123-45+6+7+8+9=108
Input
只有1个数,即整数N的值。-30000≤n≤1000000
Output
只有一行,该行只有1个数,表示能使等式成立的算式总数。
Sample Input
108
Sample Output
15
#include<bits/stdc++.h>
using namespace std;
int ans,n;
void p(int f,int u,int c,int k)
{
if(f==10)
{
if(u+c==n||c-u==n)ans++;
return;
}
p(f+1,f,u+c,f);
if(k!=1)p(f+1,f,c-u,f);
p(f+1,u*10+f,c,k);
}
int main()
{
cin>>n;
p(2,1,0,1);
cout<<ans<<endl;
}