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 火柴棒等式
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
下一篇:005 两个栈组成队列
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
用Python写算法题--洛谷P1149 火柴棒等式
用Python写算法题--洛谷P1149 火柴棒等式
Python 算法 洛谷 火柴棒等式 -
洛谷p1149
一道很有意思的题目嘞。这道题目看起来,用搜索似乎无疑了。
i++ #include d3 -
WIKIOI-1168 火柴棒等式
题目描述 Description 给你n根火柴棍,你可以拼出多少个形如“A+B=C”的等式?等式中的A、B、C是用火柴棍拼出的整数(若该数非零,则最高位不能是0)。用火柴棍拼数字0-9的拼法如图所示:注意:1. 加号与等号各自需要两根火柴棍2. 如果A≠B,则A+B=C与B+A=C视为不同的等式(A、B、C>=0)3. n根火柴棍必须全部用上 输入描述 Input De
WIKIOI 1168 火柴棒等式.tx C语言 ACM C++ OJ 输入输出 i++ #include -
[NOIP2008] 提高组 洛谷P1149 火柴棒等式
模拟 暴力枚举
#include 输入输出 打表 ios i++