(Java) 尝试使用 ArrayList 创建斐波那契数列的倍数并失败
(Java) Trying to use an ArrayList to create multiples of a Fibonacci Sequence and failing
我正在尝试我发现的一个编程挑战,你可以找到它 here 如果你想知道具体的要求是什么,但我基本上想做的是获得尽可能低的要求包含给定数字的斐波那契数列的倍数。所以输入 13 会输出 [0, 1, 1, 2, 3, 5, 8, 13]。输入 6 将输出 [0, 2, 2, 4, 6]。
我的代码适用于常规斐波那契数列中的任何数字,但它只输出任何倍数,例如,如果输入为 16,[0, 16],我不太明白为什么。任何帮助将不胜感激。
import java.util.Scanner;
import java.util.ArrayList;
public class FibonacciMultiples{
public static void main(String args[]){
int target;
ArrayList<Integer> x = new ArrayList<Integer>();
x.add(0);
Scanner input;
input = new Scanner(System.in);
System.out.println("Please enter target: ");
target = input.nextInt();
int i = 0;
int j = 1;
int k = 1;
while(x.get(i) != target){
x.add((j*k) + x.get(i));
i++;
j = x.get(i-1);
if(x.get(i) > target){
x.clear();
x.add(0);
i=0;
j=1;
k++;
}
};
System.out.println(x);
}
}
问题在这里:
j = x.get(i-1);
您从列表中获取下一次迭代的 j
,这意味着它已经乘以 k
。
然后在这里再乘以 k :
x.add((j*k) + x.get(i));
修复它的一种方法是更改
j = x.get(i-1);
至
j = x.get(i-1)/k;
编辑:
没有乘法或除法的更优雅的解决方案:
while(x.get(i) != target){
x.add(j + x.get(i));
i++;
j = x.get(i-1);
if(x.get(i) > target){
x.clear();
x.add(0);
i=0;
j=k; // this is the key
k++;
}
};
现在序列中的第一个元素被初始化为0和k,这意味着每个元素将比原始序列中对应的元素大k倍。
16 的输出:
[0, 2, 2, 4, 6, 10, 16]
一个更优雅的解决方案 (IMO) 是这样的:
public static void main(String[] args)
{
int target;
Scanner input;
input = new Scanner(System.in);
System.out.println("Please enter target: ");
target = input.nextInt();
List<Integer> fibonacciList = new ArrayList<>();
int f1 = 1; // f1 starts at 1 and is increased until found a match
do {
fibonacciList = fibonacci(f1++, target);
} while (fibonacciList.get(fibonacciList.size()-1) != target);
System.out.println(fibonacciList);
}
// calculate fibonacci with given f(1) value until
// target is reached (or passed)
public static List<Integer> fibonacci(int f1, int target)
{
List<Integer> fibonacciList = new ArrayList<>();
// seed the list with given arg
fibonacciList.add(0);
fibonacciList.add(f1);
while (fibonacciList.get(fibonacciList.size()-1) < target) {
// build the list by adding last two items
fibonacciList.add(
fibonacciList.get(fibonacciList.size()-2) +
fibonacciList.get(fibonacciList.size()-1));
}
return fibonacciList;
}
我正在尝试我发现的一个编程挑战,你可以找到它 here 如果你想知道具体的要求是什么,但我基本上想做的是获得尽可能低的要求包含给定数字的斐波那契数列的倍数。所以输入 13 会输出 [0, 1, 1, 2, 3, 5, 8, 13]。输入 6 将输出 [0, 2, 2, 4, 6]。
我的代码适用于常规斐波那契数列中的任何数字,但它只输出任何倍数,例如,如果输入为 16,[0, 16],我不太明白为什么。任何帮助将不胜感激。
import java.util.Scanner;
import java.util.ArrayList;
public class FibonacciMultiples{
public static void main(String args[]){
int target;
ArrayList<Integer> x = new ArrayList<Integer>();
x.add(0);
Scanner input;
input = new Scanner(System.in);
System.out.println("Please enter target: ");
target = input.nextInt();
int i = 0;
int j = 1;
int k = 1;
while(x.get(i) != target){
x.add((j*k) + x.get(i));
i++;
j = x.get(i-1);
if(x.get(i) > target){
x.clear();
x.add(0);
i=0;
j=1;
k++;
}
};
System.out.println(x);
}
}
问题在这里:
j = x.get(i-1);
您从列表中获取下一次迭代的 j
,这意味着它已经乘以 k
。
然后在这里再乘以 k :
x.add((j*k) + x.get(i));
修复它的一种方法是更改
j = x.get(i-1);
至
j = x.get(i-1)/k;
编辑:
没有乘法或除法的更优雅的解决方案:
while(x.get(i) != target){
x.add(j + x.get(i));
i++;
j = x.get(i-1);
if(x.get(i) > target){
x.clear();
x.add(0);
i=0;
j=k; // this is the key
k++;
}
};
现在序列中的第一个元素被初始化为0和k,这意味着每个元素将比原始序列中对应的元素大k倍。
16 的输出:
[0, 2, 2, 4, 6, 10, 16]
一个更优雅的解决方案 (IMO) 是这样的:
public static void main(String[] args)
{
int target;
Scanner input;
input = new Scanner(System.in);
System.out.println("Please enter target: ");
target = input.nextInt();
List<Integer> fibonacciList = new ArrayList<>();
int f1 = 1; // f1 starts at 1 and is increased until found a match
do {
fibonacciList = fibonacci(f1++, target);
} while (fibonacciList.get(fibonacciList.size()-1) != target);
System.out.println(fibonacciList);
}
// calculate fibonacci with given f(1) value until
// target is reached (or passed)
public static List<Integer> fibonacci(int f1, int target)
{
List<Integer> fibonacciList = new ArrayList<>();
// seed the list with given arg
fibonacciList.add(0);
fibonacciList.add(f1);
while (fibonacciList.get(fibonacciList.size()-1) < target) {
// build the list by adding last two items
fibonacciList.add(
fibonacciList.get(fibonacciList.size()-2) +
fibonacciList.get(fibonacciList.size()-1));
}
return fibonacciList;
}