基本的java递归错误(Basic java recursion error)

编程入门 行业动态 更新时间:2024-10-20 11:44:35
基本的java递归错误(Basic java recursion error) public static long fibby(long n){ if (n == 0){ return 1; } return (fibby(n/4))+(fibby(3*n/4)); } public static void sparsetablegen(int start, int end){ long fibbyOut = fibby(start); long lastFibbyOutput = fibbyOut; System.out.println(start+" "+fibbyOut); if(start != end){ sparsetablegen(start+1, end); if (lastFibbyOutput == fibbyOut){ return; } } }

免责声明:这是我的java项目的任务,我尝试了多种方法,无法找到一个有效的解决方案。 我将发布我对代码的理解以及什么不能正常工作。

我的表应该做的是接受值,从“int start”开始并在int“end”结束,这些值将由我的“fibby”函数解决。 然后它应该并排打印“start”和fibbyOut的值,直到它到达“end”。 我应该做的是跳过fibbyOut的任何重复值,例如我可能会看到:1 - > 2 2 - > 3 3 - > 4 4 - > 6 5 - > 6 6 - > 8

所以我想跳过起始值5,因为4的fibbyOut是6并且这是一个重复值。 所以相反,我应该看到1-> 2 2-> 3 3-> 4 4-> 6 6-> 8

我知道这是一个非常基本的问题,但我似乎无法想象如何删除fibbyOut的重复值。 谢谢你的帮助。

public static long fibby(long n){ if (n == 0){ return 1; } return (fibby(n/4))+(fibby(3*n/4)); } public static void sparsetablegen(int start, int end){ long fibbyOut = fibby(start); long lastFibbyOutput = fibbyOut; System.out.println(start+" "+fibbyOut); if(start != end){ sparsetablegen(start+1, end); if (lastFibbyOutput == fibbyOut){ return; } } }

Disclaimer: This is an assignment for my java project and I've tried multiple approaches and can not figure out a working solution. I will post what my understanding of the code is and what's not working properly.

What my table is supposed to do is take in values, starting from "int start" and finishing at int "end", those values will then be solved by my "fibby" function. Then it should print the values of "start" and fibbyOut side by side until it hits the "end". What I'm supposed to do is skip over any duplicate values of fibbyOut So for example I might see: 1 -> 2 2 -> 3 3 -> 4 4 -> 6 5 -> 6 6 -> 8

And so then I would want to skip over the start value 5, since fibbyOut for 4 is 6 and that's a duplicate value. So instead I should see 1-> 2 2-> 3 3-> 4 4-> 6 6-> 8

I know it's a very basic problem, but I just can't seem to figure how to remove the duplicate values of fibbyOut. Thanks for any help.

最满意答案

大规模编辑:在了解了问题的真正原因之后,我输入了以下内容:

package Main; import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { List<Long> outputs = new ArrayList<>(); table(0, 8, outputs); } public static void table(int start, int end, List<Long> outputs) { outputs.add(fibby(start)); long lastFibbyOutput = outputs.get(outputs.size() - 1); for(int i = outputs.size() - 2; i >= 0; i--) { if(outputs.size() == 1) { System.out.println(start + " " + lastFibbyOutput); //Always print the first time because it will be a unique value. break; } else if(outputs.get(i) == lastFibbyOutput) { //One of the values matches a previous one, so we break break; } //We're at the end without breaking, so we print. if(i == 0) System.out.println(start + " " + lastFibbyOutput); } if(start == end) { return; } start++; table(start, end, outputs); } public static long fibby(long n) { if(n == 0) return 1; return (fibby(n/4) + fibby(3 * n / 4)); } }

MASSIVE EDIT: After coming to the understanding of what the problem truly was, I typed out this:

package Main; import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { List<Long> outputs = new ArrayList<>(); table(0, 8, outputs); } public static void table(int start, int end, List<Long> outputs) { outputs.add(fibby(start)); long lastFibbyOutput = outputs.get(outputs.size() - 1); for(int i = outputs.size() - 2; i >= 0; i--) { if(outputs.size() == 1) { System.out.println(start + " " + lastFibbyOutput); //Always print the first time because it will be a unique value. break; } else if(outputs.get(i) == lastFibbyOutput) { //One of the values matches a previous one, so we break break; } //We're at the end without breaking, so we print. if(i == 0) System.out.println(start + " " + lastFibbyOutput); } if(start == end) { return; } start++; table(start, end, outputs); } public static long fibby(long n) { if(n == 0) return 1; return (fibby(n/4) + fibby(3 * n / 4)); } }

更多推荐

本文发布于:2023-07-04 17:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1027001.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   错误   java   error   recursion

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!