ZZNU - OJ - 2080 : A+B or A-B【暴力枚举】
2024-08-27 05:15:36
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[][N],l1,l2,l3; //num[x][y] 表示第x个字符串对应的第y个字符大小
int have[]; int legal(int s1,int s2,int s3) //合法情况只有 是个位数或者 位数等于表示的字符数
{
int len1,len2,len3;
if(s1==)len1=;
else len1=(int)log10(s1)+;
if(s2==)len2=;
else len2=(int)log10(s2)+;
if(s3==)len3=;
else len3=(int)log10(s3)+; if(l1>&&len1<l1)
return ;
if(l2>&&len2<l2)
return ;
if(l3>&&len3<l3)
return ;
return ;
}
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 ;
else
return ;
}
int fact()
{
int s1,s2,s3,ans=;
int i[],j,k; //i[1~5]依次枚举ABCDE五个数!
for(i[]=;i[]<=;i[]++){
for(i[]=;i[]<=;i[]++){
for(i[]=;i[]<=;i[]++){
for(i[]=;i[]<=;i[]++){
for(i[]=;i[]<=;i[]++){
s1=s2=s3=;
//.Each letter represents a different number
if(repeat(i[],i[],i[],i[],i[])==) //去重
continue; for(j=;j<=l1;j++) //枚举num【1】【】的每位进行组合!
s1=s1*+ i[num[][j]];
for(j=;j<=l2;j++) //枚举num【2】【】的每位进行组合!
s2=s2*+ i[num[][j]];
for(j=;j<=l3;j++) //枚举num【3】【】的每位进行组合!
s3=s3*+ i[num[][j]]; if(legal(s1,s2,s3)==){ ///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!=&&s1%s2==&&s1/s2==s3)ans++; //整数必要要用s1%s2==0,不然3/2==1!!
} for(k=;k<=;k++)//直接结束不必要的循环!!比如只有ABC,而没有DE,需要少跑两重循环
if(!have[k])i[k]=;
}
}
}
}
}
return ans;
}
int main()
{
int i,j,k,m,n;
char str[N];
while(scanf("%s",str)!=EOF)
{
memset(have,,sizeof(have));
l1=strlen(str);
for(i=;i<strlen(str);i++){
num[][i+]=str[i]-'A'+;
have[num[][i+]]=;
} scanf("%s",str);
l2=strlen(str);
for(i=;i<strlen(str);i++){
num[][i+]=str[i]-'A'+;
have[num[][i+]]=;
} scanf("%s",str);
l3=strlen(str);
for(i=;i<strlen(str);i++){
num[][i+]=str[i]-'A'+;
have[num[][i+]]=;
}
// printf("%d\n",legal(1,1,0));
printf("%d\n",fact());
} return ;
} /*样例输入
A A A
AB BA AB
A B C
样例输出
5
0
72
*/
最新文章
- EL表达式错误attribute items does not accept any expressions
- [译]在Mac上运行ASP.NET 5
- 调用Oracle存储过程并获取out参数值
- 【Flatten Binary Tree to Linked List】cpp
- RMQ(dp)
- 模板 lucas
- jQuery 基础
- hdu 2815 Mod Tree 高次方程,n不为素数
- spring security 跨域防伪攻击
- Linux学习(三)putty,xshell使用以及密匙登陆
- Microsoft Visual Studio 2012 添加实体数据模型
- 推荐 | Vue 入门&;进阶路线
- LVM备份(1)-创建LVM逻辑卷
- 微信小程序:防止多次点击跳转(函数节流)和防止表单组件输入内容多次验证(函数防抖)
- 【python】python2.x中的除法
- 路由交换02-----ARP协议
- sort逆序(char String)
- Nginx 功能模块
- Centos7下Mysql通过.frm和.ibd恢复数据
- 通过SiteMapDataSource动态获取SiteMap文件进行权限设置