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【暴力枚举】的更多相关文章

  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. 超详细的Tensorflow模型的保存和加载(理论与实战详解)

    1.Tensorflow的模型到底是什么样的? Tensorflow模型主要包含网络的设计(图)和训练好的各参数的值等.所以,Tensorflow模型有两个主要的文件: a) Meta graph: ...

  2. python解析本地HTML文件

    Python使用爬虫技术时,每运行一次,本地都会访问一次主机.为避免完成程序前调试时多次访问主机增加主机负荷,我们可以在编写程序前将网页源代码存在本地,调试时访问本地文件即可.现在我来分享一下爬取资料 ...

  3. Select 查询语句

    1.1  查询语句 1.1.1     select select 用于从数据看查询数据.语法 select field1,filed2,.. . from tablename [where cond ...

  4. Transformer的numpy实现

    下面的代码自下而上的实现Transformer的相关模块功能.这份文档只实现了主要代码.由于时间关系,我无法实现所有函数.对于没有实现的函数,默认用全大写函数名指出,如SOFTMAX 由于时间限制,以 ...

  5. 6.66 分钟,一文Python爬虫解疑大全教入门!

    我收集了大家关注爬虫最关心的  16 个问题,这里我再整理下分享给大家,并一一解答. 1. 现在爬虫好找工作吗? 如果是一年前我可能会说爬虫的工作还是挺好找的,但现在已经不好找了,一市场饱和了,二是爬 ...

  6. Word 文档内超级链接跳转到书签

    1. 前言 在Word文档内如何实现一些跳转的超链接呢?Word中,一些外部链接,我们通常叫作超链接,内部链接我们可以叫书签.如何在文档中如何使用书签,跳转到指定位置? 这里我在网上随便找了一份模拟试 ...

  7. STM32之串口硬件连接图

    笔记本USB转串口实物连接图: 电路连接图:

  8. shell从简单到脱坑

    1.计算1-100的和(seq 1 100 使用反引号括起来的比较坑) #!/bin/bash ` do sum=$[$i+$sum] done echo $sum 2.编写shell脚本,要求输入一 ...

  9. 题解-AtCoder ARC-078F Mole and Abandoned Mine

    problem ATC-arc078F 题意概要:给定一个 \(n\) 点 \(m\) 边简单无向图(无自环无重边),边有费用,现切去若干条边,使得从 \(1\) 到 \(n\) 有且仅有一条简单路径 ...

  10. entity-framework-core – 实体框架核心RC2表名称复数

    参考地址:https://docs.microsoft.com/zh-cn/ef/core/modeling/relational/tables http://www.voidcn.com/artic ...