ZZNU - OJ - 2080 : A+B or A-B【暴力枚举】

2080 : A+B or A-B(点击左侧标题进入zznu原题页面)

时间限制:1 Sec 内存限制:0 MiB
提交:8 答案正确:3

提交 状态 讨论区

题目描述

Give you three strings s1, s2 and s3. These strings contain only capital letters A,B,C,D,E. The letters that appear in the string represent a number from 0 to 9.Each letter represents a different number.Now we want to know how many four equations these strings can form. In other words, you need to calculate how many ways that s1+s2=s3
or s1-s2=s3 or s1*s2=s3 or s1/s2=s3. Notice that the leading 0 is not legal.

输入

Multiple sample input until the end of the file
The three strings represent s1, s2, s3, and their length will not exceed 5

输出

The total number of ans

样例输入

复制
A A A

样例输出

复制
5

简单分析:
Each letter represents a different number ,要求不重复枚举;
Notice that the leading 0 is not legal. 要求组合的数前面没有空0.
其余想说的话都在代码注释里:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
#define N 19
const int inf=0x3f3f3f3f;
int num[4][N],l1,l2,l3; //num[x][y] 表示第x个字符串对应的第y个字符大小
int have[6];

int legal(int s1,int s2,int s3) //合法情况只有 是个位数或者 位数等于表示的字符数
{
     int len1,len2,len3;
     if(s1==0)len1=1;
     else len1=(int)log10(s1)+1;
     if(s2==0)len2=1;
     else len2=(int)log10(s2)+1;
    if(s3==0)len3=1;
    else len3=(int)log10(s3)+1;

    if(l1>1&&len1<l1)
        return 0;
    if(l2>1&&len2<l2)
        return 0;
    if(l3>1&&len3<l3)
        return 0;
    return 1;
}
int repeat(int a,int b,int c,int d,int e){
    if(a==b||a==c||a==d||a==e||
       b==c||b==d||b==e||
       c==d||c==e||
       d==e)
        return 1;
    else
        return 0;
}
int fact()
{
    int s1,s2,s3,ans=0;
    int i[10],j,k;  //i[1~5]依次枚举ABCDE五个数!
    for(i[1]=0;i[1]<=9;i[1]++){
        for(i[2]=0;i[2]<=9;i[2]++){
            for(i[3]=0;i[3]<=9;i[3]++){
                for(i[4]=0;i[4]<=9;i[4]++){
                    for(i[5]=0;i[5]<=9;i[5]++){
                        s1=s2=s3=0;
                                    //.Each letter represents a different number
                        if(repeat(i[1],i[2],i[3],i[4],i[5])==1)  //去重
                            continue;

                        for(j=1;j<=l1;j++) //枚举num【1】【】的每位进行组合!
                            s1=s1*10+ i[num[1][j]];
                        for(j=1;j<=l2;j++) //枚举num【2】【】的每位进行组合!
                            s2=s2*10+ i[num[2][j]];
                        for(j=1;j<=l3;j++) //枚举num【3】【】的每位进行组合!
                            s3=s3*10+ i[num[3][j]];

                        if(legal(s1,s2,s3)==1){  ///Notice that the leading 0 is not legal.
                                //下面两行为调试代码,解封可以看到搜索过程!
                         //   if(s1+s2==s3||s1-s2==s3||s1*s2==s3||(s2!=0&&s1%s2==0&&s1/s2==s3))
                         //      printf("**%d)****%d %d %d\n",ans,s1,s2,s3);
                            if(s1+s2==s3)ans++;
                            if(s1*s2==s3)ans++;
                            if(s1-s2==s3)ans++;
                            if(s2!=0&&s1%s2==0&&s1/s2==s3)ans++;  //整数必要要用s1%s2==0,不然3/2==1!!
                        }

                        for(k=1;k<=5;k++)//直接结束不必要的循环!!比如只有ABC,而没有DE,需要少跑两重循环
                            if(!have[k])i[k]=10;
                    }
                }
            }
        }
    }
    return ans;
}
int main()
{
    int i,j,k,m,n;
    char str[N];
    while(scanf("%s",str)!=EOF)
    {
        memset(have,0,sizeof(have));
        l1=strlen(str);
        for(i=0;i<strlen(str);i++){
            num[1][i+1]=str[i]-'A'+1;
            have[num[1][i+1]]=1;
        }

        scanf("%s",str);
        l2=strlen(str);
        for(i=0;i<strlen(str);i++){
            num[2][i+1]=str[i]-'A'+1;
            have[num[2][i+1]]=1;
        }

        scanf("%s",str);
        l3=strlen(str);
        for(i=0;i<strlen(str);i++){
            num[3][i+1]=str[i]-'A'+1;
            have[num[3][i+1]]=1;
        }
     //  printf("%d\n",legal(1,1,0));
       printf("%d\n",fact());
    }

    return 0;
}

/*样例输入
A A A
AB BA AB
A B C
样例输出
5
0
72
*/
View Code

 

posted @ 2017-12-03 16:55  山枫叶纷飞  阅读(520)  评论(1编辑  收藏  举报