题目:​​http://acm.hdu.edu.cn/showproblem.php?pid=3699​

一开始用string超时了 

A hard Aoshu Problem(hdu 3699 暴力枚举_i++A hard Aoshu Problem(hdu 3699 暴力枚举_i++_02


#include<bits/stdc++.h>
using namespace std;
char s1[10],s2[10],s3[10];
bool qdl(char s[],int a,int b,int c,int d,int e)//前导零
{
if(strlen(s)>1&&(s[0]=='A'&&a==0||s[0]=='B'&&b==0||s[0]=='C'&&c==0||s[0]=='D'&&d==0||s[0]=='E'&&e==0))return 0;
return 1;
}
int change(char s[],int a,int b,int c,int d,int e)//字符串 转int
{
int q=0,p;
for(int i=0;i<strlen(s);i++)
{
if(s[i]=='A')p=a;
else if(s[i]=='B')p=b;
else if(s[i]=='C')p=c;
else if(s[i]=='D')p=d;
else p=e;
q=q*10+p;
}
return q;
}
int main()
{
int a,b,c,d,e;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s%s%s",s1,s2,s3);
int mp[100];
memset(mp,0,sizeof(mp));
for(int k=0;s1[k];k++)mp[s1[k]]++;
for(int k=0;s2[k];k++)mp[s2[k]]++;
for(int k=0;s3[k];k++)mp[s3[k]]++;
int cnt=0;//几个字母没出现过
for(int i=65;i<=69;i++)if(mp[i]==0)cnt++;
int ans=0;
for(a=0;a<=9;a++)
{
for(b=0;b<=9;b++)
{
if(b!=a)
{
for(c=0;c<=9;c++)
{
if(c!=a&&c!=b)
{
for(d=0;d<=9;d++)
{
if(d!=a&&d!=b&&d!=c)
{
for(e=0;e<=9;e++)
{
if(e!=a&&e!=b&&e!=c&&e!=d)
{
if(qdl(s1,a,b,c,d,e)&&qdl(s2,a,b,c,d,e)&&qdl(s3,a,b,c,d,e))
{
int x=change(s1,a,b,c,d,e);
int y=change(s2,a,b,c,d,e);
int z=change(s3,a,b,c,d,e);
if(x+y==z)ans++;
if(x-y==z)ans++;
if(x*y==z)ans++;
if(y!=0&&x%y==0&&x/y==z)ans++;
}
}
}
}
}
}
}
}
}
}
for(int i=6;i<cnt+6;i++)
{
ans/=i;//把没出现过的字母循环算进去了 所以除去
}
printf("%d\n",ans);
}
return 0;
}
/*
2
A A A
BCD BCD B
*/

View Code