计算多项式

时间:2018-04-23 13:43:19

标签: c

多项式是a0x ^ 0 + a1x ^ 1 + a2x ^ 2 + a3x ^ 3 + ... + anx ^ n 你能帮帮我吗?

 int degreeOfPolynomial;
double[] coefficients;

public Polynomial() {

}

public Polynomial(int degree) {

    for(int loopingVariable = 0; loopingVariable<=getDegree(); loopingVariable++) {
        this.coefficients[loopingVariable] = 0;
    }
}

4 个答案:

答案 0 :(得分:0)

按相反顺序:

int poly = 13;
int power = 0;
int mask = 1;
boolean nothing = true;
while (mask <= poly) {
    if ((mask & poly) != 0) {
        if (nothing) {
            nothing = false;
        } else {
            System.out.print(" + ");
        }
        if (power == 0) {
            System.out.print("1");
        } else if (power == 1) {
            System.out.print("x");
        } else {
            System.out.print("x^" + power);
        }
    }
    ++power;
    mask <<= 1;
}
System.out.println();

按照正确的顺序:

int poly = 13;
int power = 0;
int mask = 1;
boolean nothing = true;
while (mask <= poly) {
    mask <<= 1;
    ++power;
}
while (power > 0) {
    --power;
    mask >>= 1;
    if ((mask & poly) != 0) {
        if (nothing) {
            nothing = false;
        } else {
            System.out.print(" + ");
        }
        if (power == 0) {
            System.out.print("1");
        } else if (power == 1) {
            System.out.print("x");
        } else {
            System.out.print("x^" + power);
        }
    }
}
System.out.println();

答案 1 :(得分:0)

编辑:我对此进行了测试,它根据您问题的第一种形式工作,其中没有x的系数。

为了将它应用于您目前所处的问题,您必须同时检查字符串和系数数组并将其关联起来。

String binaryNumber = Integer.toBinaryString(number);
String polynomial = "";
for(int i=0;i<binaryNumber.length();i++){
   if(binaryNumber.charAt(i)==('1')){   
      if(polynomial.length()>0){
         // We already have some value in the polynomial so we add a + before the new values
         polynomial += "+";
         }
    // We append the new x together with its appropriate exponent based on some index arithmetics
    polynomial += "x^"+((binaryNumber.length()-1)-i);
    }
}

System.out.println(polynomial);

答案 2 :(得分:0)

试试这个:

public class Main {
    public static void main(String[] args) {
        int number = 13;
        String binaryString = Integer.toBinaryString(number); // prints 1101

        StringBuilder stringBuilder = new StringBuilder();
        for (int index = 0; index < binaryString.length(); index++) {
            int pow = binaryString.length() - 1 - index; // the pow

            if (binaryString.charAt(index) == '1') {   // skip when the bit is 0
                String toInsert;
                if (pow == 0) {  
                    toInsert = "1"; // when pow is 0, append 1
                } else if (pow == 1) {  
                    toInsert = "x"; // when pow is 1, append x
                } else {
                    toInsert = "x^" + pow; // else append x^pow
                }
                stringBuilder.append(toInsert + "+");
            }
        }

        String result = stringBuilder.toString();
        if (result.endsWith("+")) {
            result = result.substring(0, result.length() - 1); // remove last "+"
        }
        System.out.println(result); // x^3 + x^2 + 1
    }
}

答案 3 :(得分:0)

BigInteger具有比大多数其他数值更好的位操作功能。

public static String asPoly(long n) {
    return asPoly(BigInteger.valueOf(n));
}

public static String asPoly(BigInteger b) {
    // Using BigInteger because it has a bitLength.
    List<String> parts = new ArrayList<>();
    for (int p = b.bitLength(); p >= 0; p--) {
        if (b.testBit(p)) {
            switch (p) {
                default:
                    parts.add("x^" + p);
                    break;
                case 1:
                    parts.add("x");
                    break;
                case 0:
                    parts.add("1");
                    break;
            }
        }
    }
    return String.join(" + ",parts);
}

public void test(String[] args) {
    int number = 15;
    System.out.println(Integer.toBinaryString(number));
    System.out.println(asPoly(number));
}