- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我希望创建一个程序来识别数字中的某些模式。我不确定这是否需要算法或只是经过深思熟虑的编程。我不是在寻找提供源代码的人,只是在寻找一些发人深省的想法,让我朝着正确的方向前进。
数字将固定长度为 6 位数字,从 000000 到 999999。我猜每个数字都将存储为数组的一部分。然后我想根据模式测试数字。
例如,假设我使用的 3 种模式是
A A A A A A - would match such examples as 111111 , 222222, 333333 etc where
A B A B A B - would match such examples as 121212 , 454545, 919191 etc
A (A+1) (A+2) B (B+1) (B+2) - would match such examples as 123345, 789123, 456234
我想我卡住的部分是如何将整数数组的每个部分分配给一个值,例如 A 或 B
我最初的想法只是将每个部分分配为一个单独的字母。因此,如果数组由 1 3 5 4 6 8 组成,那么我将创建一个像
这样的 mapA=1
B=3
C=5
D=4
E=6
F=8
然后一些如何采取第一个模式,
AAAAAA
并用类似 if (AAAAAA = ABCDEF) 的东西进行测试,然后我们匹配了 AAAAAAA
如果不是,则尝试 (ABABAB = ABCDEF) 等遍历我的所有模式
在这种情况下,分配给 C 的值没有理由不能与分配给 F 的值相同,例如数字 234874。
我不确定这是否对任何人都有意义,但我想我可以根据反馈完善我的问题。
总而言之,我正在寻找有关如何让程序接受 6 位数字并返回给我们它匹配的模式的想法。
解决方案
在给出的评论让我走上正轨之后,下面是我创建的最终解决方案。
package com.doyleisgod.number.pattern.finder;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class FindPattern {
private final int[] numberArray; //Array that we will match patterns against.
private final Document patternTree = buildPatternTree(); //patternTree containing all the patterns
private final Map<String, Integer> patternisedNumberMap; //Map used to allocate ints in the array to a letter for pattern analysis
private int depth = 0; //current depth of the pattern tree
// take the int array passed to the constructor and store it in out numberArray variable then build the patternised map
public FindPattern (int[] numberArray){
this.numberArray = numberArray;
this.patternisedNumberMap = createPatternisedNumberMap();
}
//builds a map allocating numbers to letters. map is built from left to right of array and only if the number does not exist in the map does it get added
//with the next available letter. This enforces that the number assigned to A can never be the same as the number assigned to B etc
private Map<String, Integer> createPatternisedNumberMap() {
Map<String, Integer> numberPatternMap = new HashMap<String, Integer>();
ArrayList<String> patternisedListAllocations = new ArrayList<String>();
patternisedListAllocations.add("A");
patternisedListAllocations.add("B");
patternisedListAllocations.add("C");
patternisedListAllocations.add("D");
Iterator<String> patternisedKeyIterator = patternisedListAllocations.iterator();
for (int i = 0; i<numberArray.length; i++){
if (!numberPatternMap.containsValue(numberArray[i])) {
numberPatternMap.put(patternisedKeyIterator.next(), numberArray[i]);
}
}
return numberPatternMap;
}
//Loads an xml file containing all the patterns.
private Document buildPatternTree(){
Document document = null;
try {
File patternsXML = new File("c:\\Users\\echrdoy\\Desktop\\ALGO.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
document = db.parse(patternsXML);
} catch (Exception e){
e.printStackTrace();
System.out.println("Error building tree pattern");
}
return document;
}
//gets the rootnode of the xml pattern list then called the dfsnodesearch method to analyse the pattern against the int array. If a pattern is found a
//patternfound exception is thorwn. if the dfsNodeSearch method returns without the exception thrown then the int array didn't match any pattern
public void patternFinder() {
Node rootnode= patternTree.getFirstChild();
try {
dfsNodeSearch(rootnode);
System.out.println("Pattern not found");
} catch (PatternFoundException p) {
System.out.println(p.getPattern());
}
}
//takes a node of the xml. the node is checked to see if it matches a pattern (this would only be true if we reached the lowest depth so must have
//matched a pattern. if no pattern then analyse the node for an expression. if expression is found then test for a match. the int from the array to be tested
//will be based on the current depth of the pattern tree. as each depth represent an int such as depth 0 (i.e root) represent position 0 in the int array
//depth 1 represents position 1 in the int array etc.
private void dfsNodeSearch (Node node) throws PatternFoundException {
if (node instanceof Element){
Element nodeElement = (Element) node;
String nodeName = nodeElement.getNodeName();
//As this method calls its self for each child node in the pattern tree we need a mechanism to break out when we finally reach the bottom
// of the tree and identify a pattern. For this reason we throw pattern found exception allowing the process to stop and no further patterns.
// to be checked.
if (nodeName.equalsIgnoreCase("pattern")){
throw new PatternFoundException(nodeElement.getTextContent());
} else {
String logic = nodeElement.getAttribute("LOGIC");
String difference = nodeElement.getAttribute("DIFFERENCE");
if (!logic.equalsIgnoreCase("")&&!difference.equalsIgnoreCase("")){
if (matchPattern(nodeName, logic, difference)){
if (node.hasChildNodes()){
depth++;
NodeList childnodes = node.getChildNodes();
for (int i = 0; i<childnodes.getLength(); i++){
dfsNodeSearch(childnodes.item(i));
}
depth--;
}
}
}
}
}
}
//for each node at a current depth a test will be performed against the pattern, logic and difference to identify if we have a match.
private boolean matchPattern(String pattern, String logic, String difference) {
boolean matched = false;
int patternValue = patternisedNumberMap.get(pattern);
if (logic.equalsIgnoreCase("+")){
patternValue += Integer.parseInt(difference);
} else if (logic.equalsIgnoreCase("-")){
patternValue -= Integer.parseInt(difference);
}
if(patternValue == numberArray[depth]){
matched=true;
}
return matched;
}
}
模式的 xml 列表如下所示
<?xml version="1.0"?>
<A LOGIC="=" DIFFERENCE="0">
<A LOGIC="=" DIFFERENCE="0">
<A LOGIC="=" DIFFERENCE="0">
<A LOGIC="=" DIFFERENCE="0">
<pattern>(A)(A)(A)(A)</pattern>
</A>
<B LOGIC="=" DIFFERENCE="0">
<pattern>(A)(A)(B)(A)</pattern>
</B>
</A>
<B LOGIC="=" DIFFERENCE="0">
<A LOGIC="=" DIFFERENCE="0">
<pattern>(A)(A)(B)(A)</pattern>
</A>
<B LOGIC="=" DIFFERENCE="0">
<pattern>(A)(A)(B)(B)</pattern>
</B>
</B>
</A>
<A LOGIC="+" DIFFERENCE="2">
<A LOGIC="+" DIFFERENCE="4">
<A LOGIC="+" DIFFERENCE="6">
<pattern>(A)(A+2)(A+4)(A+6)</pattern>
</A>
</A>
</A>
<B LOGIC="=" DIFFERENCE="0">
<A LOGIC="+" DIFFERENCE="1">
<B LOGIC="+" DIFFERENCE="1">
<pattern>(A)(B)(A+1)(B+1)</pattern>
</B>
</A>
<A LOGIC="=" DIFFERENCE="0">
<A LOGIC="=" DIFFERENCE="0">
<pattern>(A)(B)(A)(A)</pattern>
</A>
<B LOGIC="+" DIFFERENCE="1">
<pattern>(A)(B)(A)(B+1)</pattern>
</B>
<B LOGIC="=" DIFFERENCE="0">
<pattern>(A)(B)(A)(B)</pattern>
</B>
</A>
<B LOGIC="=" DIFFERENCE="0">
<A LOGIC="=" DIFFERENCE="0">
<pattern>(A)(B)(B)(A)</pattern>
</A>
<B LOGIC="=" DIFFERENCE="0">
<pattern>(A)(B)(B)(B)</pattern>
</B>
</B>
<A LOGIC="-" DIFFERENCE="1">
<B LOGIC="-" DIFFERENCE="1">
<pattern>(A)(B)(A-1)(B-1)</pattern>
</B>
</A>
</B>
<A LOGIC="+" DIFFERENCE="1">
<A LOGIC="+" DIFFERENCE="2">
<A LOGIC="+" DIFFERENCE="3">
<pattern>(A)(A+1)(A+2)(A+3)</pattern>
</A>
</A>
</A>
<A LOGIC="-" DIFFERENCE="1">
<A LOGIC="-" DIFFERENCE="2">
<A LOGIC="-" DIFFERENCE="3">
<pattern>(A)(A-1)(A-2)(A-3)</pattern>
</A>
</A>
</A>
</A>
我的模式发现异常类如下所示
package com.doyleisgod.number.pattern.finder;
public class PatternFoundException extends Exception {
private static final long serialVersionUID = 1L;
private final String pattern;
public PatternFoundException(String pattern) {
this.pattern = pattern;
}
public String getPattern() {
return pattern;
}
}
不确定这是否会对遇到类似问题的任何人有所帮助,或者如果有人对它的工作方式有任何意见,我会很高兴听到他们的意见。
最佳答案
我建议构建状态机:
一个。使用模式初始化:
B.运行状态机
A.1。模式归一化。
A A A A A A => A0 A0 A0 A0 A0 A0
C A C A C A => A0 B0 A0 B0 A0 B0(始终以 A 开头,然后是 B、C、D 等)
B B+1 B+2 A A+1 A+2 => A0 A1 A2 B0 B1 B2
因此,您始终拥有以 A0 开头的规范化模式。
A.2。建一棵树
1. A0
/ | \
2. A0 B0 A1
| | |
3. A0 A0 A2
| | |
4. A0 B0 B0
| | |
5. A0 A0 B1
| | |
6. A0 B0 B2
| | |
p1 p2 p3
B.运行状态机
使用Depth-first search algorithm使用递归查找匹配的模式。
有意义吗?
关于用于识别数字模式的 Java 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11422498/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!