我有以下是計算位置斐波那契數10的代碼:
IntStream.range(1,10)
.mapToObj(ignore -> List.Of(BigInteger.One, BigInteger.One)
.reduce(List.Of(BigInteger.One, BigInteger.One), (values,ignore) -> ???)
.get(1)
如何替換代碼中的問號?
我必須通過僅使用函式式編程來實作它。
uj5u.com熱心網友回復:
您需要的 lambda 是:
(values, ignore) -> List.of(values.get(1), values.get(0).add(values.get(1)))
它將串列轉換[a, b]為[b, a b].
但是,您的代碼中有一些編譯錯誤:
- 這
List.of()不是List.Of() - 這
BigInteger.ONE不是BigInteger.One
還有一個邏輯錯誤:第一個斐波那契數是 0,而不是 1,所以要回傳以 開頭的第 n 個斐波那契數IntStream.range(1, n),累加器必須是List.of(BigInteger.ZERO, BigInteger.ONE),而不是List.of(BigInteger.ONE, BigInteger.ONE)。
完整的作業代碼是:
BigInteger fibonacci = IntStream.range(1, 10)
.mapToObj(ignore -> List.of(BigInteger.ZERO))
.reduce(
List.of(BigInteger.ZERO, BigInteger.ONE),
(values, ignore) -> List.of(values.get(1), values.get(0).add(values.get(1)))
).get(0);
它回傳34,這是第 10 個斐波那契數。
而不是 use IntStream#range(),一種更簡單的方法是使用上面的相同 lambdaStream#generate()生成起始串列和 apply limit(n):
BigInteger fibonacci = Stream.generate(() -> List.of(BigInteger.ZERO, BigInteger.ONE))
.limit(10)
.reduce((values, ignore) -> List.of(values.get(1), values.get(0).add(values.get(1))))
.get().get(0);
也歸來34。
uj5u.com熱心網友回復:
如果您打算將此代碼用于相對較小的輸入數字,例如10,使用BigInteger似乎不合理。
否則,為序列的每個成員創建一個新串列在性能方面是有害的。最好只創建一次串列然后重復使用它。
不幸的是List.of()回傳一個不可變的串列,為了使它可變,你用一個ArrayList笨拙的代碼包裝它變得更加笨拙......
相反,我建議使用如下所示的陣列,它會使代碼更高效且更易于推理。
public static BigInteger getFib(int num) {
return Stream.iterate(new BigInteger[]{BigInteger.ZERO, BigInteger.ONE}, // the only array created in the stream
arr -> {arr[1] = arr[0].add(arr[1]); // calculating the next member of the sequence
arr[0] = arr[1].subtract(arr[0]); // calculating the previous member of the sequence
return arr;}) // returning the same array
.limit(num - 1)
.reduce((left, right) -> right)
.map(arr -> arr[1])
.orElse(BigInteger.ZERO);
}
main()
public static void main(String[] args) {
List<BigInteger> result = // generating a list Fibonacci numbers for demo purposes
IntStream.rangeClosed(1, 10)
.mapToObj(num -> getFib(num))
.collect(Collectors.toList());
System.out.println(result);
}
輸出
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
uj5u.com熱心網友回復:
要計算Nth斐波那契數列的項,您可以這樣做。不涉及任何串列,因為它們是動態計算的,并且只有Nth一個被列印或回傳。這通過初始化一個陣列并生成隨后的陣列a來作業,這些陣列被用作系列中的術語。如果不足,可以更改的位置開始。[ 0, 1 ][ a[1], a[0] a[1] ]a[0]1
- 定義位置(必須 >= 1)或(即 >= 第一個位置)
- 使用陣列迭代系列。
- 跳過
position - 1流中的第一個術語。 - 然后列印下一個(或分配它)
int position = 10;
Stream.iterate(new BigInteger[] { BigInteger.ZERO, BigInteger.ONE },
(a) -> new BigInteger[] { a[1], a[0].add(a[1]) })
.skip(position-1).limit(1)
.map(a->a[0]).forEach(System.out::println);
}
印刷
34
分配它更改forEach并findFirst().get().分配給一個BigInteger。
在這里,我將其包含在一個方法中。
public static BigInteger getFibTerm(int position) {
if (position < 1) {
throw new IllegalArgumentException("position must be >= 1");
}
return Stream.iterate(
new BigInteger[] { BigInteger.ZERO, BigInteger.ONE },
(a) -> new BigInteger[] { a[1], a[0].add(a[1]) })
.skip(position - 1).limit(1).map(a -> a[0])
.findFirst().get();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/452501.html
