您的位置:首页 > 其它

noip2008提高组——火柴棒等式

2015-02-08 10:24 218 查看


这道题算法上 就是裸dfs 吧

主要是看代码能力。。(这是一个悲伤的故事。。QAQ)

细节上只要注意0不可以开头就好了

有一个小优化

把加号和等号一起看做分隔符号

就只用数分割号的个数 模拟起来就好做多了

#include<cstdio>
#include<cstring>
using namespace std;
const int use[]={6,2,5,5,4,5,6,3,7,6,2};
int n,ans;
char cal[100];
bool getnum(int len,int &now,int &i,int jh)
{
int bz=0;
while(1)
{
if(cal[i]>'9')
break;
if(i==len+1)
{
bz=1;break;
}
if(now==-1)
{
now=cal[i]-'0';
i++;
continue;
}
now*=10;
now+=cal[i]-'1'+1;
i++;
}
if(jh==1)
bz=0;
if(now==-1||bz==1)
return false;
i++;
return true;
}
void work()
{
int now=-1,now1=-1,res=-1;
int len=strlen(cal)-1;
int cnt=0;
for(int i=0;i<=len;i++)
if(cal[i]>'9')
cnt++;
if(cnt>2)
return;
int i=0;
int bz=0;
if(!getnum(len,now,i,0))
return;
if(!getnum(len,now1,i,0))
return;
if(!getnum(len,res,i,1))
return;
if(res==now+now1)
ans++;
}
void solve(int depth,int now)
{
if(depth>n)
return;
if(depth==n)
{
work();
return;
}
for(int i=0;i<=10;i++)
{
if(cal[now-1]=='0'&&i==0)
continue;
if(cal[now-1]=='0'&&i!=10)
continue;
cal[now]=i+'1'-1;
solve(depth+use[i],now+1);
cal[now]=0;
}
}
int main()
{
scanf("%d",&n);
solve(0,0);
printf("%d",ans);
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: