/*
题目1498:寻找表达式
直接深搜复杂度 3^14次方
*/
#include <stdio.h>
int n;
int op[20];
bool judge()
{
int ans;
if(op[1]==0)
ans=0;
else ans=1;
int i=1;
int temp;
while(i<n)
{
if(op[i]==0)
{
temp=i;
while(i<n && op[i]==0)
{
i++;
if(i>=10)
temp=temp*100+i;
else
temp=temp*10+i;
}
ans+=temp;
}
else if(op[i]==1)
{
temp=++i;
while(i<n && op[i]==0)
{
i++;
if(i>=10)
temp=temp*100+i;
else
temp=temp*10+i;
}
ans+=temp;
}
else
{
temp=++i;
while(i<n && op[i]==0)
{
i++;
if(i>=10)
temp=temp*100+i;
else
temp=temp*10+i;
}
ans-=temp;
}
}
if(ans==0)
return 1;
return 0;
}
void prit()
{
for(int i=1; i<n; i++)
{
if(op[i]==0)
printf("%d ",i);
else if(op[i]==1)
printf("%d+",i);
else
printf("%d-",i);
}
printf("%d\n",n);
}
void dfs(int x)
{
if(x==n)
{
if(judge())
prit();
return ;
}
op[x]=0;
dfs(x+1);
op[x]=1;
dfs(x+1);
op[x]=2;
dfs(x+1);
}
int main()
{
// printf("%d %d %d\n",' ','+','-');
while(scanf("%d",&n)!=EOF)
{
dfs(1);
}
return 0;
}
题目1498:寻找表达式
最新推荐文章于 2023-11-17 02:01:13 发布