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

样例输入

复制
  1. A A A

样例输出

复制
  1. 5
  2.  
  3. 简单分析:
    Each letter represents a different number ,要求不重复枚举;
    Notice that the leading 0 is not legal. 要求组合的数前面没有空0.
    其余想说的话都在代码注释里:
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<algorithm>
  4. #include<string.h>
  5. #include<stdlib.h>
  6. #include<math.h>
  7. using namespace std;
  8. #define N 19
  9. const int inf=0x3f3f3f3f;
  10. int num[][N],l1,l2,l3; //num[x][y] 表示第x个字符串对应的第y个字符大小
  11. int have[];
  12.  
  13. int legal(int s1,int s2,int s3) //合法情况只有 是个位数或者 位数等于表示的字符数
  14. {
  15. int len1,len2,len3;
  16. if(s1==)len1=;
  17. else len1=(int)log10(s1)+;
  18. if(s2==)len2=;
  19. else len2=(int)log10(s2)+;
  20. if(s3==)len3=;
  21. else len3=(int)log10(s3)+;
  22.  
  23. if(l1>&&len1<l1)
  24. return ;
  25. if(l2>&&len2<l2)
  26. return ;
  27. if(l3>&&len3<l3)
  28. return ;
  29. return ;
  30. }
  31. int repeat(int a,int b,int c,int d,int e){
  32. if(a==b||a==c||a==d||a==e||
  33. b==c||b==d||b==e||
  34. c==d||c==e||
  35. d==e)
  36. return ;
  37. else
  38. return ;
  39. }
  40. int fact()
  41. {
  42. int s1,s2,s3,ans=;
  43. int i[],j,k; //i[1~5]依次枚举ABCDE五个数!
  44. for(i[]=;i[]<=;i[]++){
  45. for(i[]=;i[]<=;i[]++){
  46. for(i[]=;i[]<=;i[]++){
  47. for(i[]=;i[]<=;i[]++){
  48. for(i[]=;i[]<=;i[]++){
  49. s1=s2=s3=;
  50. //.Each letter represents a different number
  51. if(repeat(i[],i[],i[],i[],i[])==) //去重
  52. continue;
  53.  
  54. for(j=;j<=l1;j++) //枚举num【1】【】的每位进行组合!
  55. s1=s1*+ i[num[][j]];
  56. for(j=;j<=l2;j++) //枚举num【2】【】的每位进行组合!
  57. s2=s2*+ i[num[][j]];
  58. for(j=;j<=l3;j++) //枚举num【3】【】的每位进行组合!
  59. s3=s3*+ i[num[][j]];
  60.  
  61. if(legal(s1,s2,s3)==){ ///Notice that the leading 0 is not legal.
  62. //下面两行为调试代码,解封可以看到搜索过程!
  63. // if(s1+s2==s3||s1-s2==s3||s1*s2==s3||(s2!=0&&s1%s2==0&&s1/s2==s3))
  64. // printf("**%d)****%d %d %d\n",ans,s1,s2,s3);
  65. if(s1+s2==s3)ans++;
  66. if(s1*s2==s3)ans++;
  67. if(s1-s2==s3)ans++;
  68. if(s2!=&&s1%s2==&&s1/s2==s3)ans++; //整数必要要用s1%s2==0,不然3/2==1!!
  69. }
  70.  
  71. for(k=;k<=;k++)//直接结束不必要的循环!!比如只有ABC,而没有DE,需要少跑两重循环
  72. if(!have[k])i[k]=;
  73. }
  74. }
  75. }
  76. }
  77. }
  78. return ans;
  79. }
  80. int main()
  81. {
  82. int i,j,k,m,n;
  83. char str[N];
  84. while(scanf("%s",str)!=EOF)
  85. {
  86. memset(have,,sizeof(have));
  87. l1=strlen(str);
  88. for(i=;i<strlen(str);i++){
  89. num[][i+]=str[i]-'A'+;
  90. have[num[][i+]]=;
  91. }
  92.  
  93. scanf("%s",str);
  94. l2=strlen(str);
  95. for(i=;i<strlen(str);i++){
  96. num[][i+]=str[i]-'A'+;
  97. have[num[][i+]]=;
  98. }
  99.  
  100. scanf("%s",str);
  101. l3=strlen(str);
  102. for(i=;i<strlen(str);i++){
  103. num[][i+]=str[i]-'A'+;
  104. have[num[][i+]]=;
  105. }
  106. // printf("%d\n",legal(1,1,0));
  107. printf("%d\n",fact());
  108. }
  109.  
  110. return ;
  111. }
  112.  
  113. /*样例输入
  114. A A A
  115. AB BA AB
  116. A B C
  117. 样例输出
  118. 5
  119. 0
  120. 72
  121. */

ZZNU - OJ - 2080 : A+B or A-B【暴力枚举】的更多相关文章

  1. [Swust OJ 763]--校门外的树 Plus(暴力枚举)

    题目链接:http://acm.swust.edu.cn/problem/0763/ Time limit(ms): 1000 Memory limit(kb): 65535 西南某科技大学的校门外有 ...

  2. CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。

    1514: Packs Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 61  Solved: 4[Submit][Status][Web Board] ...

  3. BestCoder Round #50 (div.1) 1002 Run (HDU OJ 5365) 暴力枚举+正多边形判定

    题目:Click here 题意:给你n个点,有多少个正多边形(3,4,5,6). 分析:整点是不能构成正五边形和正三边形和正六边形的,所以只需暴力枚举四个点判断是否是正四边形即可. #include ...

  4. sdut oj 2372 Annoying painting tool (【暴力枚举测试】1Y )

    Annoying painting tool 题目描述 Maybe you wonder what an annoying painting tool is? First of all, the pa ...

  5. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

  6. 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&& ...

  7. 【学术篇】oj.jzxx.net2701 无根树

    这是一道来自OIerBBS的题目.. 原帖地址:http://www.oierbbs.com/forum.php?mod=viewthread&tid=512?fromuid=71 (似乎是个 ...

  8. Comet OJ - Contest #5

    Comet OJ - Contest #5 总有一天,我会拿掉给\(dyj\)的小裙子的. A 显然 \(ans = min(cnt_1/3,cnt_4/2,cnt5)\) B 我们可以感性理解一下, ...

  9. POJ题目分类(按初级\中级\高级等分类,有助于大家根据个人情况学习)

    本文来自:http://www.cppblog.com/snowshine09/archive/2011/08/02/152272.spx 多版本的POJ分类 流传最广的一种分类: 初期: 一.基本算 ...

随机推荐

  1. 时空卷积网络TCN

    1.写在前面 实验表明,RNN 在几乎所有的序列问题上都有良好表现,包括语音/文本识别.机器翻译.手写体识别.序列数据分析(预测)等. 在实际应用中,RNN 在内部设计上存在一个严重的问题:由于网络一 ...

  2. 【Tools】PDF编辑软件-pdfelement 6.8 官网文件中文+破解版本

    试用了下,感觉还不错分享给大家. 有币的求赏,小弟下载缺币.没币的从附件下载. 赏币地址:https://download.csdn.net/download/qq_18187161/10744059 ...

  3. [BJOI2019] 奥术神杖 [取log+AC自动机+dp]

    题面 传送门 思路 首先,看到这个乘起来开根号的形式,应该能想到用取$\log$的方式做一个转化: $\sqrt[n]{\prod_i a_i}=\frac{1}{n}\sum_i \log_b a_ ...

  4. picard报错

    /home/yueyao/bio/anaconda2/bin/java -jar /home/yueyao/bio/anaconda2/share/picard-2.14.1-0/picard.jar ...

  5. [转帖]什么是UWB?UWB有什么用?

    什么是UWB?UWB有什么用? https://www.sohu.com/a/224891573_531173 小米碰传 就是 UWB吧? 2018-03-05 17:02 UWB在早期被用来应用在近 ...

  6. SpringBoot中service注入失败(A component required a bean of type 'XXService' that could not found)

    先写了JUnit,发现启动不了,注释掉有问题的service也不可以.可能是因为spring开始时会加载所有service吧. 按照网友们的说法,一般需要检查: 1.入口类有没有写MapperScan ...

  7. AVR单片机教程——点亮第一个LED

    做了这么多准备,我们终于可以开始用开发板做点事了. 单片机编程与计算机编程有一些不同点.程序都要有零个或多个输入.一个或多个输出,这是两者都有的,但是计算机编程的输入输出主要靠控制台,而单片机没有. ...

  8. closed channel

    func Test_chanel(t *testing.T) { c := make(chan int, 1) go func() { time.Sleep(time.Second * 3) clos ...

  9. golang 单元测试(一)

    单元测试函数类型 Test(功能测试) 函数规则: 函数名: TestXxxx , 以Test为前缀.Xxxx以大写字母开头 参数类型: *testing.T func TestXxxx(t *tes ...

  10. 【并发】9、借助redis 实现生产消费,消息订阅发布模式队列

    这个就是一个消息可以被多次消费的范例了 其实这个实现的方式可以参考我之前的设计模式,观察者模式 https://www.cnblogs.com/cutter-point/p/5249780.html ...