【问题标题】:Representation of an array as a polynomial将数组表示为多项式
【发布时间】:2021-05-23 18:56:07
【问题描述】:

这个方法应该做的是获取一个双精度数组并将它们转换为一个多项式,例如,如果给定数组是[2.0, 3.0, -2.0],则该方法的输出将是2.0x^2 + 3.0x^1 - 2.0。我所做的是创建了两个循环,但是当我这样做时,两个输出都按预期分开。我的输出看起来像这样2.0 1.0 2.0 1.0 x^3 x^2 x^1,如果有办法在系数之后打印指数来解决这个问题。

public String printPoly(Double[] doubles) {
    String polynomialString = "";

    for (int i = 0; i < doubles.length; i++) {
        polynomialString += doubles[i] + " ";
    }

    for (int j = doubles.length - 1; j >= 0; j--) {
        if (j == 0) {
            polynomialString += " ";
            break;
        }
        polynomialString += "x^" + j + " ";
    }
    return polynomialString;
}

【问题讨论】:

    标签: java string loops methods


    【解决方案1】:

    您可以使用一个 for 循环构建 resultString。 初始化一个您倒数的计数器变量,它代表您的 ^3 部分。 这个变量从你的 array.length-1 开始,在迭代中运行到零,这就是你可以创建的方式

    4.0 x^3
    2.0 x^2
    0.5 x^1
    2.0 x^0
    

    它基本上是向后运行的,所以你应该在每次迭代中递减它

    int backCounter = doubles.length-1;
    

    在你的一个 for 循环中做

    polynomialString += doubles[j] + "x^" + backCounter + " ";
    
    backCounter--;
    

    这是一个可行的解决方案

    package so;
    
    import java.util.*;
    
    public class RemoveString {
    
        public static void main(String[] args) {
            Double[] someNumbers = { 2.0, 3.0, 1.0, 0.5 };
            String s = RemoveString.printPoly(someNumbers);
            System.out.println(s);
        }
    
        public static String printPoly(Double[] doubles) {
    
            String polynomialString = "";
            int backwardsCounter = doubles.length - 1;
    
            for (int i = 0; i < doubles.length; i++) {
                polynomialString += doubles[i] + "x^" + backwardsCounter + " ";
                backwardsCounter --;
            }
    
            return polynomialString;
        }
    }
    

    产生输出

    2.0x^3 3.0x^2 1.0x^1 0.5x^0 
    

    【讨论】:

    • 所以事实证明我们不希望它打印 x^0,而是我们只想打印系数,这可以通过 if 语句来完成,对吧?
    • yes 检查最后一次迭代 i == doubles.length -1 然后只追加 doubles[i]
    【解决方案2】:

    这里一个loop就足够了,或者一个stream

    public static void main(String[] args) {
        System.out.println(printPoly(new double[]{2.0, 3.0, -2.0}));
        // 2.0x^2 + 3.0x^1 - 2.0
        System.out.println(printPoly(new double[]{-2.0, 3.6, -2.1, 5.2}));
        // -2.0x^3 + 3.6x^2 - 2.1x^1 + 5.2
    }
    
    public static String printPoly(double[] doubles) {
        return IntStream
                // iterate through the array indices
                .range(0, doubles.length)
                // string representation of the coefficient
                .mapToObj(i -> coefficient(i, doubles))
                // concatenate into one string
                .collect(Collectors.joining());
    }
    
    // separate method because inside the lambda it is too big
    static String coefficient(int i, double[] doubles) {
        String val = "";
        // take value from an array
        double element = doubles[i];
        if (i == 0) {
            // first element
            val += element;
        } else {
            // other elements
            if (element <= 0.0D) {
                val += " - " + (0.0D - element);
            } else {
                val += " + " + element;
            }
        }
        // if non-last element, then
        // raised to the power of
        if (i < (doubles).length - 1) {
            // inverted index
            val += "x^" + (doubles.length - 1 - i);
        }
        return val;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-26
      • 1970-01-01
      • 2019-11-08
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 2023-03-14
      • 2014-03-30
      相关资源
      最近更新 更多