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. logstash kafka output 日志处理

    今天在用logstash解析日志并传送给kafka的时候,发现kafka能收到数据但数据内容已经丢失,具体如下: 在logstash output中增加输出样式之后,问题解决kafka再次接受到的内容 ...

  2. iOS 获取设备的唯一标识

    有时候,我们需要记录一下设备的唯一标识,比如标识这个设备是不是已经发过促销券了或者是否下载试用过app等等.最简单 的方法就是获取设备的UDID#[UIDevice currentDevice] un ...

  3. React-native/React 公告滚动组件(原生代码)

    编写不易, 希望大家点赞 import React, {PureComponent} from 'react'; import {Animated, Easing, View} from 'react ...

  4. 热力图heatmap使用

    参考:https://www.cnblogs.com/julinhuitianxia/p/7755246.html 1.首先到echarts官网下载应用实例:http://echarts.baidu. ...

  5. ubuntu 安装 typora

    # or run: # sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys BA300B7755AFCFAE wget -qO ...

  6. canal启动报错ERROR c.a.o.canal.parse.inbound.mysql.dbsync.DirectLogFetcher - I/O error while reading from client socket

  7. MacOS上使用Openconnect代替Cisco Anyconnect

    OpenConnect是一个Cisco Anyconnect的替代品,具有开源.易获取.可靠等优点.而官方版本的Cisco Anyconnect配置较为繁琐,需要在管理界面同时部署多平台客户端才能支持 ...

  8. [VS] - 手工打开 WCF 客户端调试工具

    操作步骤 1. 在开始菜单中找到 Visual Studio 命令行工具 2. 输入命令 wcftestclient 即可打开 WCF 客户端测试工具 参考资料http://www.cnblogs.c ...

  9. java8 : 流

    package day02.com.offcn.test; import java.io.IOException; import java.nio.charset.Charset; import ja ...

  10. Python11之列表2(获取、删除列表元素、列表分片、拷贝)

    一.获取列表元素值 列表名 [ 索引值 ] 注:索引值从0开始 nameList = ['詹姆斯','字母哥','乐福','威少','乔治','戴维斯'] nameList[0] '詹姆斯' name ...