有因子的三项式/多项式

时间:2018-02-09 03:14:06

标签: java polynomials quadratic jcreator factoring

所以我想创建一个程序,当用户输入一个值c和a = 1时,该程序打印出一个可以考虑的二次方程式。程序应该确定b的所有可能的整数值,以便三项式以x ^ 2 + bx + c

的形式打印出来

例如,如果用户为c输入-4,程序应该打印出来:
x ^ 2 - 4
x ^ 2 - 3x - 4

到目前为止,这是我对我的代码所做的,我试图弄清楚如何执行程序,但我真的很难从这里去哪里。如果有人能提供一些非常感谢的帮助!

public class FactorableTrinomials
{
    public static void main (String [] args)
    {
        Scanner scan = new Scanner(System.in);


        System.out.println("A trinomial in standard form is ax^2 + bx + 
        c. \nIf a = 1, this program will output all factorable trinomials 
        given the entered c value.");

        System.out.print("\nEnter an integer “c” value: ");
        int numC = scan.nextInt();

        final int NUMA= 1;
        int numB;


        if (numC > 0)
        {
            int factors;

            System.out.print("\nThe factors of " + numC + " are: ");

            for(factors = 1; factors <= numC; factors++)    //determines 
            factors and how many there are
            {
                if(numC % factors == 0)
                {
                    System.out.print(factors + " ");
                }
            }

1 个答案:

答案 0 :(得分:0)

首先,找到与c相乘的整数对。 那么b的所有可能值都将是整数对的总和。

查找整数对的简单方法是将2个变量从-c循环到c并检查产品是否为c。例如:

for(int i = -1 * numC; i <= numC; i++) {
    for(int j = -1* numC; j<= numC;j++) {
        if(i * j == numC) {
            int b = i + j;

            //print solution, if b == 0 then don't print the second term
        }
    }
}