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-> 3-> 4-> 6-> 8
我知道这是一个非常基本的问题,但我似乎无法理解如何删除fibbyOut的重复值。谢谢你的帮助。
发布于 2017-05-21 17:04:22
大量编辑:在了解了问题的真正所在之后,我输入了以下内容:
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));
}
}
https://stackoverflow.com/questions/44103217
复制相似问题