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. 进程和线程—Python多线程编程

    进程和线程 进程 进程是一个执行中的程序.每个进程都拥有自己的地址空间.内存.数据栈以及其它用于跟踪执行的辅助数据. 一个程序运行就是一个进程(比如 QQ.微信或者其它软件): 进程可以通过派生新的进 ...

  2. ql的python学习之路-day12

    前言:这一节主要学习json和pickle 背景: 相信大家在日常生活中都有接触大型的网络游戏,打游戏的时候都是自己在电脑上操作,自己刷怪升级:当然也会碰到中午去吃饭然后挂机的情况,让电脑自动的刷怪, ...

  3. css3及css技巧

    左右对齐:  

  4. Kubernetes实战 - 从零开始搭建微服务 1 - 使用kind构建一个单层架构Node/Express网络应用程序

    使用kind构建一个单层架构Node/Express网络应用程序 Kubernetes实战-从零开始搭建微服务 1 前言 准备写一个Kubernetes实战系列教程,毕竟cnblogs作为国内最早的技 ...

  5. day03: copy的总结(20170215)

    import copynames = ["88xiaoming","liuhai","杨东","liuhai",&quo ...

  6. 干货分享:如何使用Kubernetes的Ingress API

    您可以通过使用诸如Kong for Kubernetes的Ingress控制器(使用自定义资源定义并提供许多插件)来极大地扩展Ingress资源的功能. Kubernetes正在整个技术行业中得到采用 ...

  7. C# DataTable转为TXT文档

    public static void SaveCSV(DataTable dt, string fullPath) { var fi = new FileInfo(fullPath); if (!fi ...

  8. 【python----发轫之始】【基础知识总结】

    python基础知识总结 一.自学感受 学完之后,,,感觉脑子里全是乱的,单词这么多,都要分不清什么时候该用什么,他到底属于哪一个数据类型里的函数,,,,,, 所以,我想着把笔记整理一下,方便自己和需 ...

  9. 存储系列之 RAID技术原理简介

    引言:RAID技术是现代大规模存储的基础,“基础(技术)是拿来革命的”.我查raid相关资料时,查布尔运算,竟然一路查到“香农原理”,这不是有个视频中HW的任总提到的吗,多基础的东西,任总却毫不含糊, ...

  10. 王艳 201771010127《面向对象程序设计(java)》第三周学习总结

    一:理论知识总结: 第一章:主要概述了java相比其他程序设计语言(如C语言.c++)之间的不同性能.为我们揭示了java这种语言的设计初衷一节截至目前java语言达到的效果.另外,还简要介绍了jav ...