import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner; public class Main {
private static Scanner cin;
private static LinkedList<Integer>[] list;
private static HashMap equationCalced;
private static HashMap calculatedN;
public static void init() {
list=new LinkedList[6];
list[0] = new LinkedList<Integer>();
list[0].add(1);//2 match can represent 1
list[1] = new LinkedList<Integer>();
list[1].add(7);//3 match can represent 7
list[2] = new LinkedList<Integer>();
list[2].add(4);//4 match can represent 4
list[3] = new LinkedList<Integer>();
list[3].add(2);//5 match can represent 2
list[3].add(3);//5 match can represent 3
list[3].add(5);//5 match can represent 5
list[4] = new LinkedList<Integer>();
list[4].add(0);//6 match can represent 0
list[4].add(6);//6 match can represent 6
list[4].add(9);//6 match can represent 9
list[5] = new LinkedList<Integer>();
list[5].add(8);//7 match can represent 8 equationCalced = new HashMap();
calculatedN = new HashMap();
} public static void main(String args[]) throws Exception {
cin = new Scanner(System.in);
int n = cin.nextInt();
Main.init();
int matchesForDigit = n-4;
//we need at least 12 matches, the equation is 1+1=2
if(n<13) {
System.out.println(0);
}else {
System.out.println(calc(matchesForDigit));
}
} //calc digit which can be represented by n matches
public static int calc(int n) {
//LinkedList<Integer> digitList = new LinkedList<Integer>();
int ret = 0;
//get the first digit
for(int i=2;i<=n-4;i++) {
LinkedList<String> listA = digitWithMatchN(i);
if(null == listA) {
continue;
} for(int j=2;j<=n-i-2;j++) {
LinkedList<String> listB = digitWithMatchN(j);
LinkedList<String> listC = digitWithMatchN(n-i-j);
if (null == listB || null == listC) {
continue;
}
Object[] arrayA = listA.toArray();
Object[] arrayB = listB.toArray();
Object[] arrayC = listC.toArray();
for(int x=0;x<arrayA.length;x++) {
for(int y=0;y<arrayB.length;y++) {
for(int z=0;z<arrayC.length;z++) {
long a = Long.valueOf((String)arrayA[x]);
long b = Long.valueOf((String)arrayB[y]);
long c = Long.valueOf((String)arrayC[z]);
if(a + b == c) {
if (equationCalced.containsKey(String.format("%d+%d", a,b))) {
continue;
}else {
ret++;
equationCalced.put(String.format("%d+%d", a,b), c);
}
}
}
}
}
}
}
return ret;
} public static LinkedList<String> digitWithMatchN(int n) {
if(calculatedN.containsKey(n)) {
return (LinkedList<String>)calculatedN.get(n);
}
LinkedList<String> retList = new LinkedList<String>();
if(n == 0) {
return null;
}else if(n==1) {
//this match list can not present the right digit
return null;
}else if(n == 2){
//return digit 1
retList.add("1");
}else if(n == 3) {
//return digit 7
retList.add("7");
}else {
for(int i=2;i<=7 && i<=n;i++) {
Iterator<Integer> ai = list[i-2].iterator();
//iterate digit in list[i-2]
if(0 == n-i) {
while(ai.hasNext()) {
String tmpStr = String.valueOf(ai.next());
retList.add(tmpStr);
}
}else {
LinkedList<String> tmp = digitWithMatchN(n-i);
if(null == tmp) {// n-1 match can not represent the right digit
continue;
}
while(ai.hasNext()) {
String tmpStr = String.valueOf(ai.next());
Iterator tmpIt = tmp.iterator();
boolean hasNext = false;
while (tmpIt.hasNext()) {
hasNext = true;
String tmpStr2 = (String)tmpIt.next();
if(tmpStr.equals("0")) {
break;//if the prefix is 0, this digit is not illegal
}else {
retList.add(tmpStr +tmpStr2);
}
}
if (!hasNext) {
retList.add(tmpStr);
}
}
}
}
}
if(!calculatedN.containsKey(n)) {
calculatedN.put(n, retList);
}
return retList;
}
}

Java实现 洛谷 P1149 火柴棒等式的更多相关文章

  1. 用Python写算法题--洛谷P1149 火柴棒等式

    题目 题目来源 P1149 火柴棒等式,https://www.luogu.org/problem/P1149 题目描述 给你n根火柴棍,你可以拼出多少个形如"A+B=C"的等式? ...

  2. [NOIP2008] 提高组 洛谷P1149 火柴棒等式

    题目描述 给你n根火柴棍,你可以拼出多少个形如“A+B=C”的等式?等式中的A.B.C是用火柴棍拼出的整数(若该数非零,则最高位不能是0).用火柴棍拼数字0-9的拼法如图所示: 注意: 加号与等号各自 ...

  3. 洛谷P1149 火柴棒等式

    题目描述 给你n根火柴棍,你可以拼出多少个形如“A+B=C”的等式?等式中的A.B.C是用火柴棍拼出的整数(若该数非零,则最高位不能是0).用火柴棍拼数字0-9的拼法如图所示: 注意: 1.加号与等号 ...

  4. 洛谷 P1149 火柴棒等式

    嗯....   这道题好讨厌啊!!!!   一开始莫名RE,然后发现数组小了,然后发现后面几个点总是WA,原来推的少了....   并且这道题的思路真的好水啊!!   先看一下题: 题目描述 给你n根 ...

  5. 洛谷P1149.火柴棒等式(暴力搜索)

    题目描述 给你n根火柴棍,你可以拼出多少个形如"A+B=C"的等式?等式中的A.B.C是用火柴棍拼出的整数(若该数非零,则最高位不能是0).用火柴棍拼数字0-9的拼法如图所示: 注 ...

  6. (水题)洛谷 - P1149 - 火柴棒等式

    https://www.luogu.org/problemnew/show/P1149 一开始还分类重复了.在非0的dfs中居然赋值了0,脑残得一笔. 其实就按 $lead0$ 分类就好了, $lea ...

  7. luogu P1149 火柴棒等式

    题目描述 给你n根火柴棍,你可以拼出多少个形如“A+B=C”的等式?等式中的A.B.C是用火柴棍拼出的整数(若该数非零,则最高位不能是0).用火柴棍拼数字0-9的拼法如图所示: 注意: 加号与等号各自 ...

  8. (函数)P1149 火柴棒等式

    题解: #include<stdio.h>int a[10]={6,2,5,5,4,5,6,3,7,6};int num(int n){                          ...

  9. [折腾笔记] 洛谷P1149-火柴棒等式 AC记

    原题链接: https://www.luogu.org/problem/P1149 题面简述: 给你n根火柴棍,你可以拼出多少个形如"A+B=C""A+B=C" ...

随机推荐

  1. Docker学习笔记:镜像、容器、数据卷

    核心概念 镜像:一个只读的模板,类似虚拟机的镜像. 容器:可以理解为镜像的一个运行实例.运行时类似于沙箱,多个容器互相独立. 仓库:存放镜像文件的地方. 镜像 命令表格 命令 解释 选项 docker ...

  2. docker部署微服务遇到的问题二

    自己尝试将微服务部署到docker上面,期间按照周立的微服务架构实战13章进行学习 按照书上的步骤,一切部署成功之后,尝试访问,一直没有成功访问,周五部署了两遍 折腾了一下 还是没有找到为啥,周一继续 ...

  3. spring MVC--WebApplicationContext做了什么

    在WebApplicationContext中默认内置了DispatcherServlet依赖的bean!我们可以根据实际的项目需要对这些bean进行自定义参数设置.因为如果在配置文件中存在我们自定义 ...

  4. FF按钮点击后表单提交

    如果发现<button>提交</button>点击后,所在的表单在ff中自动提交了,则需要添加属性 type='button'! 我也是百度的,记在这里以后方便查看!

  5. 200万年薪请不到!清华姚班到底有多牛X?

    前几天,清华大学自动化系2020年大一新生的C++作业因为太难而上了热搜,该话题在知乎上的热度一度高达 1300+ 万.  在该帖子下方,有很多关于这件事的讨论,其中很多不禁赞叹"清华太牛 ...

  6. Python格式化字符串(格式化输出)

    熟悉C语言 printf() 函数的读者能够轻而易举学会 Python print() 函数,它们是非常类似的. print() 函数使用以%开头的转换说明符对各种类型的数据进行格式化输出,具体请看下 ...

  7. Proteus仿真时出现Cannot open‘C:\Users\...\LISA7605.SDF’的错误

    目录 打开环境变量 更改环境变量 打开环境变量 更改环境变量 "用户变量"和"系统变量"栏里,找到TEMP与TMP,都改成%SystemRoot%\TEMP 改 ...

  8. 数独c++

    #include <bits/stdc++.h> using namespace std; const int maxn = 10; bool maps[maxn][maxn], row[ ...

  9. urllib全解

    Urllib库的基本使用 转载1 博客园  python修行路:https://www.cnblogs.com/zhaof/p/6910871.html 转载2csdn          原文链接:h ...

  10. iozone测试报错:Error writing block 12634, fd= 3 write: No space left on device

    问题:使用iozone测试GFS的读写性能的时候,一直报错Error writing block 12634, fd= 3 write: No space left on device,百思不得其解: ...