Java 我';我在这个查找唯一路径数的算法中出错了吗?
问题陈述: 我有2个输入文件来测试这个算法 第一: 我得到了这个输出: 案例#1:5 (这个不错) 第二: 我得到了这个输出: 案例#1:1 案例2:1 案例#3:9 案例4:4 个案#5:101 案例6:3.125 问题:所有案例都可以,但第6个案例是错误的 我正在使用以下代码:Java 我';我在这个查找唯一路径数的算法中出错了吗?,java,algorithm,Java,Algorithm,问题陈述: 我有2个输入文件来测试这个算法 第一: 我得到了这个输出: 案例#1:5 (这个不错) 第二: 我得到了这个输出: 案例#1:1 案例2:1 案例#3:9 案例4:4 个案#5:101 案例6:3.125 问题:所有案例都可以,但第6个案例是错误的 我正在使用以下代码: public class DemoApplication { private static final Logger LOGGER = Logger.getLogger("com.example.de
public class DemoApplication {
private static final Logger LOGGER = Logger.getLogger("com.example.demo.DemoApplication");
private static final String TEMPLATE = "Case #{0}: {1}\r\n";
private static final String PLANET_START = "Galactica";
private static final String PLANET_END = "New Earth";
public static void main(String args[]) {
String inputFilePath = "testInput.txt";
String outputFilePath = "output.txt";
try (BufferedReader reader = Files.newBufferedReader(Paths.get(inputFilePath))) { //Create the reader with the file path of testInput.txt
try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(outputFilePath))) { //Create the writer with the file path we want the output
String line = reader.readLine();//skip first row
int i = 0;//Counter of lines
Map<String, String[]> planets = null;
int caseNumber = 0;
while ((line = reader.readLine()) != null) {//Get the value of the current line
if (!isNumeric(line)) {
String[] split = line.split(":");//Split planet name and the paths
String planetName = split[0];
String[] connections = split[1].split(",");//Split different planets
planets.put(planetName, connections);
i++;
} else {
if (i > 0) {
writeFile(writer, planets, ++caseNumber);
}
planets = new HashMap<>();//reset
i = 0;//reset
}
}
writeFile(writer, planets, ++caseNumber);
}
} catch (IOException e) {
LOGGER.log(Level.WARNING, MessageFormat.format("IOException: {0}", e));
}
}
private static boolean isNumeric(String input) {
Pattern p = Pattern.compile("\\d+\\.?\\d*");
return p.matcher(input).matches();
}
private static int differentPaths(Map<String, String[]> planets, String planetName) {
if (planetName.equals(PLANET_END)) {//Last planed to arrive
return 1;
} else {
int accumulator = 0;
String[] paths = planets.get(planetName);
for (int i = 0; i < paths.length; i++) {
accumulator += differentPaths(planets, paths[i]);
}
return accumulator;
}
}
private static void writeFile(BufferedWriter writer, Map<String, String[]> planets, int caseNumber) throws IOException {
int result = differentPaths(planets, PLANET_START);//First planet from we start
writer.write(MessageFormat.format(TEMPLATE, caseNumber, result));//Write in file
}
}
公共类演示应用程序{
私有静态最终记录器Logger=Logger.getLogger(“com.example.demo.DemoApplication”);
私有静态最终字符串模板=“Case#{0}:{1}\r\n”;
私有静态最终字符串PLANET_START=“卡拉狄加”;
私人静态最终字符串行星_END=“新地球”;
公共静态void main(字符串参数[]){
字符串inputFilePath=“testInput.txt”;
字符串outputFilePath=“output.txt”;
try(BufferedReader=Files.newBufferedReader(path.get(inputFilePath)){//使用testInput.txt文件路径创建读取器
try(BufferedWriter writer=Files.newBufferedWriter(path.get(outputFilePath)){//使用我们想要输出的文件路径创建writer
String line=reader.readLine();//跳过第一行
int i=0;//行的计数器
地图行星=空;
int caseNumber=0;
while((line=reader.readLine())!=null){//获取当前行的值
如果(!isNumeric(行)){
String[]split=line.split(:“”;//分割行星名称和路径
字符串planetName=split[0];
String[]connections=split[1]。split(“,”;//拆分不同的行星
planetName.put(planetName,connections);
i++;
}否则{
如果(i>0){
writeFile(writer,planets,++案例编号);
}
planets=新建HashMap();//重置
i=0;//重置
}
}
writeFile(writer,planets,++案例编号);
}
}捕获(IOE异常){
log(Level.WARNING,MessageFormat.format(“IOException:{0}”,e));
}
}
私有静态布尔值isNumeric(字符串输入){
模式p=Pattern.compile(“\\d+\\.?\\d*”);
返回p.matcher(输入).matches();
}
专用静态整数差分路径(地图行星、字符串行星名称){
如果(planetName.equals(PLANET_END)){//最后一个到达的飞机
返回1;
}否则{
int累加器=0;
String[]path=planets.get(planetName);
for(int i=0;i
我做错了什么?如果可能的话,我更愿意解释为什么这不适用于案例6,而不是给我解决方案。这是因为我正在努力学习算法
这个问题是tuenti挑战的第二个问题()如果有人感兴趣,我想你仍然可以参加
提前谢谢
解决方案:
更改将输出转换结果写入字符串的方法,执行此操作时,MessageFormat.format不会将点添加为十进制分隔符
private static void writeFile(BufferedWriter writer, Map<String, String[]> planets, int caseNumber) throws IOException {
int result = differentPaths(planets, PLANET_START);//First planet from we start
writer.write(MessageFormat.format(TEMPLATE, caseNumber, Integer.toString(result)));//Write in file
}
private static void writeFile(BufferedWriter writer、Map plants、int caseNumber)抛出IOException{
int result=differentitpath(行星,行星开始);//从我们开始的第一个行星
writer.write(MessageFormat.format(模板、caseNumber、Integer.toString(结果));//写入文件
}
您的算法似乎运行良好,可能问题出在MessageFormat.format方法中,3.125可能是3125,而点只是千位分隔符。请发布准确的问题陈述。你对问题的解释不够充分,也很难理解。我在编辑帖子时遇到问题,无法在上面添加问题陈述,而是添加了问题陈述的链接。非常抱歉,这是问题所在