这道题是递归与暴力的结合。首先是需要进行递归将每一个数学表达式存在数组里,然后将每一个表达式通过代码实现进行数学运算,判断结果是否为零,为零就输出。然后递归下一个表达式。代码如下。
#include<bits/stdc++.h>
using namespace std;
char pro[20],sign[4]={' ',' ','+','-'};
int n=0,cnt=0,ans=1;
int ansjudge(){
int p[20],cnt=1;
memset(p,0,sizeof(p));
p[cnt]=pro[1]-'0';
for (int i=1;i<n;i++){
if (pro[2*i]==' '){
p[cnt]=p[cnt]*10+(pro[2*i+1]-'0');
}
else if (pro[2*i]=='+'){
cnt++;
p[cnt]=2;
cnt++;
p[cnt]=pro[2*i+1]-'0';
}
else if (pro[2*i]=='-'){
cnt++;
p[cnt]=3;
cnt++;
p[cnt]=pro[2*i+1]-'0';
}
}
int ans=p[1];
for (int i=1;i<=cnt/2;i++){
if (p[2*i]==2){
ans=ans+p[2*i+1];
}
else if (p[2*i]==3){