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[][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
- */
ZZNU - OJ - 2080 : A+B or A-B【暴力枚举】的更多相关文章
- [Swust OJ 763]--校门外的树 Plus(暴力枚举)
题目链接:http://acm.swust.edu.cn/problem/0763/ Time limit(ms): 1000 Memory limit(kb): 65535 西南某科技大学的校门外有 ...
- CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。
1514: Packs Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 61 Solved: 4[Submit][Status][Web Board] ...
- BestCoder Round #50 (div.1) 1002 Run (HDU OJ 5365) 暴力枚举+正多边形判定
题目:Click here 题意:给你n个点,有多少个正多边形(3,4,5,6). 分析:整点是不能构成正五边形和正三边形和正六边形的,所以只需暴力枚举四个点判断是否是正四边形即可. #include ...
- sdut oj 2372 Annoying painting tool (【暴力枚举测试】1Y )
Annoying painting tool 题目描述 Maybe you wonder what an annoying painting tool is? First of all, the pa ...
- LeetCode OJ 题解
博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...
- USACO Section 1.3 题解 (洛谷OJ P1209 P1444 P3650 P2693)
usaco ch1.4 sort(d , d + c, [](int a, int b) -> bool { return a > b; }); 生成与过滤 generator&& ...
- 【学术篇】oj.jzxx.net2701 无根树
这是一道来自OIerBBS的题目.. 原帖地址:http://www.oierbbs.com/forum.php?mod=viewthread&tid=512?fromuid=71 (似乎是个 ...
- Comet OJ - Contest #5
Comet OJ - Contest #5 总有一天,我会拿掉给\(dyj\)的小裙子的. A 显然 \(ans = min(cnt_1/3,cnt_4/2,cnt5)\) B 我们可以感性理解一下, ...
- POJ题目分类(按初级\中级\高级等分类,有助于大家根据个人情况学习)
本文来自:http://www.cppblog.com/snowshine09/archive/2011/08/02/152272.spx 多版本的POJ分类 流传最广的一种分类: 初期: 一.基本算 ...
随机推荐
- 时空卷积网络TCN
1.写在前面 实验表明,RNN 在几乎所有的序列问题上都有良好表现,包括语音/文本识别.机器翻译.手写体识别.序列数据分析(预测)等. 在实际应用中,RNN 在内部设计上存在一个严重的问题:由于网络一 ...
- 【Tools】PDF编辑软件-pdfelement 6.8 官网文件中文+破解版本
试用了下,感觉还不错分享给大家. 有币的求赏,小弟下载缺币.没币的从附件下载. 赏币地址:https://download.csdn.net/download/qq_18187161/10744059 ...
- [BJOI2019] 奥术神杖 [取log+AC自动机+dp]
题面 传送门 思路 首先,看到这个乘起来开根号的形式,应该能想到用取$\log$的方式做一个转化: $\sqrt[n]{\prod_i a_i}=\frac{1}{n}\sum_i \log_b a_ ...
- picard报错
/home/yueyao/bio/anaconda2/bin/java -jar /home/yueyao/bio/anaconda2/share/picard-2.14.1-0/picard.jar ...
- [转帖]什么是UWB?UWB有什么用?
什么是UWB?UWB有什么用? https://www.sohu.com/a/224891573_531173 小米碰传 就是 UWB吧? 2018-03-05 17:02 UWB在早期被用来应用在近 ...
- SpringBoot中service注入失败(A component required a bean of type 'XXService' that could not found)
先写了JUnit,发现启动不了,注释掉有问题的service也不可以.可能是因为spring开始时会加载所有service吧. 按照网友们的说法,一般需要检查: 1.入口类有没有写MapperScan ...
- AVR单片机教程——点亮第一个LED
做了这么多准备,我们终于可以开始用开发板做点事了. 单片机编程与计算机编程有一些不同点.程序都要有零个或多个输入.一个或多个输出,这是两者都有的,但是计算机编程的输入输出主要靠控制台,而单片机没有. ...
- closed channel
func Test_chanel(t *testing.T) { c := make(chan int, 1) go func() { time.Sleep(time.Second * 3) clos ...
- golang 单元测试(一)
单元测试函数类型 Test(功能测试) 函数规则: 函数名: TestXxxx , 以Test为前缀.Xxxx以大写字母开头 参数类型: *testing.T func TestXxxx(t *tes ...
- 【并发】9、借助redis 实现生产消费,消息订阅发布模式队列
这个就是一个消息可以被多次消费的范例了 其实这个实现的方式可以参考我之前的设计模式,观察者模式 https://www.cnblogs.com/cutter-point/p/5249780.html ...