BigInteger_問題
樣本_輸出
給定以下介面類
interface BigOperation
{
public BigInteger operation (Biglnteger x, Biglnteger y);
}
撰寫 Lambda 運算式來實作 BigOperation,以模擬 Biglnteger 上的三個算術運算 / 和 %。您可以分別稱它們為 op1、op2 和 op3。請注意,BigInteger 中的三個運算分別稱為乘法、除法和余數。使用這三個操作,我們希望將 BigInteger 中的所有數字相乘。例如,如果 Biglnteger 為 234,則乘積為 2 * 3 * 4 = 24。該方法具有以下方法標題:
static Biglnteger product (BigInteger n, BigOperation op1, BigOperation op2,
BigOperation op3)
使用兩種方法比較此方法的實作:迭代和遞回。將 Lambda 運算式放在 main 方法中。構造幾個 Biglnteger 物件,呼叫方法來測驗我們的設計。下面顯示了一些示例輸出:
Given big integer 8584803
(Iterative) e
(Recursive) 0
Given big integer 12345
(Iterative) 120
(Recursive) 120
我不知道如何在我所附的代碼之后繼續處理代碼。我想嘗試BigInteger N使用foreach回圈,但它似乎不起作用。接下來,我想做一個for(int i = 0)等回圈,但我不知道如何獲取對BigInteger變數的位置參考。我不知道如何為此使用 lambda。
預先感謝您回答我的問題。
import java.math.BigInteger;
import java.util.Random;
interface BigOperation{
public BigInteger operation(BigInteger x,BigInteger y);
}
class LearningBigInteger {
static BigInteger product(BigInteger n,BigOperation op1, BigOperation op2, BigOperation op3){
}
public static void main(String[] args) {
BigOperation multiply = BigInteger::multiply;
BigOperation divide = BigInteger::divide;
BigOperation remainder = BigInteger::remainder;
Random r = new Random();
BigInteger n = BigInteger.valueOf(Math.abs(r.nextInt()));
BigInteger m = product(n,multiply,divide,remainder);
}
}
uj5u.com熱心網友回復:
先上代碼。代碼后出現說明。
import java.math.BigInteger;
public class LearningBigInteger {
private static BigOperation multiply;
private static BigOperation divide;
private static BigOperation remainder;
private static BigInteger digitProduct(BigInteger x) {
if (x.compareTo(BigInteger.TEN) <= 0) {
return x;
}
else {
BigInteger r = remainder.operation(x, BigInteger.TEN);
BigInteger newX = divide.operation(x, BigInteger.TEN);
return multiply.operation(r, digitProduct(newX));
}
}
public static void main(String[] args) {
multiply = (x, y) -> x.multiply(y);
divide = (x, y) -> x.divide(y);
remainder = (x, y) -> x.remainder(y);
BigInteger x = new BigInteger("8584803");
BigInteger product = BigInteger.ONE;
while (x.compareTo(BigInteger.TEN) >= 0) {
BigInteger r = remainder.operation(x, BigInteger.TEN);
product = multiply.operation(product, r);
x = divide.operation(x, BigInteger.TEN);
}
System.out.println(product);
System.out.println(digitProduct(new BigInteger("12345")));
}
}
interface BigOperation {
public BigInteger operation(BigInteger x,BigInteger y);
}
介面BigOperation被稱為功能介面,因為它只包含一個 [抽象] 方法。
lambda 運算式是在函式式介面中撰寫唯一方法的實作的一種速記方式。
寫 lambda 運算式的方法是先寫方法的引數串列,然后是箭頭符號,最后是方法體。根據方法引數的數量和方法主體中的行數,語法略有不同。在上面的代碼中,語法是針對有兩個引數的方法,方法體只有一行。如果只有一個方法引數或方法體中有不止一行,則語法會有所不同。
您問題中的代碼使用方法參考,我想這是一種特殊形式的lambda 運算式。
在上面的代碼中,方法main包含迭代方法,而方法digitProduct實作遞回方法。
uj5u.com熱心網友回復:
嘗試這個。
interface BigOperation {
BigInteger operation(BigInteger x, BigInteger y);
}
static class Iterative {
static BigInteger product(BigInteger x, BigOperation op1, BigOperation op2, BigOperation op3) {
BigInteger product = BigInteger.ONE;
for ( ; x.compareTo(BigInteger.ZERO) > 0; x = op2.operation(x, BigInteger.TEN))
product = op1.operation(product, op3.operation(x, BigInteger.TEN));
return product;
}
}
static class Recursive {
static BigInteger product(BigInteger x, BigOperation op1, BigOperation op2, BigOperation op3) {
if (x.compareTo(BigInteger.TEN) < 0)
return x;
else
return op1.operation(op3.operation(x, BigInteger.TEN),
product(op2.operation(x, BigInteger.TEN), op1, op2, op3));
}
}
和
static void test(BigInteger x, BigOperation op1, BigOperation op2, BigOperation op3) {
System.out.println("Given bit integer " x);
System.out.println("(Iterative) " Iterative.product(x, op1, op2, op3));
System.out.println("(Recursive) " Recursive.product(x, op1, op2, op3));
}
public static void main(String[] args) {
BigOperation multiply = BigInteger::multiply;
BigOperation divide = BigInteger::divide;
BigOperation remainder = BigInteger::remainder;
test(new BigInteger("8584803"), multiply, divide, remainder);
test(new BigInteger("12345"), multiply, divide, remainder);
}
輸出:
Given bit integer 8584803
(Iterative) 0
(Recursive) 0
Given bit integer 12345
(Iterative) 120
(Recursive) 120
uj5u.com熱心網友回復:
class LearningBigInteger {
// Iterable version
static BigInteger product(BigInteger n, BigOperation... ops) {
BigInteger temp = n;
for (BigOperation op : ops) {
// I only have temp to work it
// so it is being processed
// to itself, pointless i know!
temp = op.operation(temp, temp);
}
return temp;
}
// Discrete version
static BigInteger product(BigInteger n,BigOperation op1, BigOperation op2, BigOperation op3){
BigInteger temp = n;
// This layout is good
// if you keep the same temp
// variable as input!
temp = op1.operation(temp, temp);
temp = op2.operation(temp, temp);
temp = op3.operation(temp, temp);
return temp;
}
}
我也打不開你提供的鏈接。修復后通知我!
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/371679.html
上一篇:圖搜索,找到值時如何停止搜索?
下一篇:使用Yield壓平字典
