You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:仅支持正系数的C++二次方程因式分解代码优化

Hey there! Let's fix that quadratic factorization code of yours so it handles negative coefficients smoothly—those are super common in quadratics, so we're gonna make your code robust enough for all cases.

First, let's assume your original code looks something like this (since you didn't share it, this is a typical version that only works with positive coefficients):

#include <iostream>
using namespace std;

void factorizeQuadratic(int a, int b, int c) {
    // Only checks positive factors
    for (int i = 1; i <= a; i++) {
        if (a % i != 0) continue;
        int j = a / i;
        for (int k = 1; k <= c; k++) {
            if (c % k != 0) continue;
            int l = c / k;
            if (i*l + j*k == b) {
                cout << "(" << i << "x + " << k << ")(" << j << "x + " << l << ")" << endl;
                return;
            }
        }
    }
    cout << "Cannot be factorized into integer coefficients." << endl;
}

int main() {
    int a, b, c;
    cout << "Enter coefficients a, b, c: ";
    cin >> a >> b >> c;
    factorizeQuadratic(a, b, c);
    return 0;
}

The core issue here is that this code only checks positive factor pairs. When coefficients like b or c are negative, it can't find the right combination of factors to match the equation.


Solution: Handle Positive & Negative Factors

We need to account for all possible sign combinations of factors, since the signs of b and c directly affect what our factor pairs look like:

  • If c is positive, the constant terms in the factors are either both positive or both negative.
  • If c is negative, the constant terms have opposite signs.
  • The sign of b tells us whether positive terms dominate (for same-sign constants) or which sign is larger (for opposite-sign constants).

Here's the revised code that handles all these cases:

#include <iostream>
#include <cmath> // For absolute value checks
using namespace std;

void factorizeQuadratic(int a, int b, int c) {
    // Iterate over all positive and negative factors of a
    for (int i = -abs(a); i <= abs(a); i++) {
        if (i == 0 || a % i != 0) continue;
        int j = a / i;
        
        // Iterate over all positive and negative factors of c
        for (int k = -abs(c); k <= abs(c); k++) {
            if (k == 0 || c % k != 0) continue;
            int l = c / k;
            
            // Check if this factor pair produces the correct b coefficient
            if (i*l + j*k == b) {
                // Format output to avoid messy signs like "+ -3"
                cout << "(";
                if (i != 1 && i != -1) cout << i;
                else if (i == -1) cout << "-";
                cout << "x";
                if (k > 0) cout << " + " << k;
                else if (k < 0) cout << " - " << abs(k);
                cout << ")(";
                
                if (j != 1 && j != -1) cout << j;
                else if (j == -1) cout << "-";
                cout << "x";
                if (l > 0) cout << " + " << l;
                else if (l < 0) cout << " - " << abs(l);
                cout << ")" << endl;
                
                return;
            }
        }
    }
    cout << "Cannot be factorized into integer coefficients." << endl;
}

int main() {
    int a, b, c;
    cout << "Enter coefficients a, b, c (for ax² + bx + c): ";
    cin >> a >> b >> c;
    factorizeQuadratic(a, b, c);
    return 0;
}

Key Improvements
  • Full factor coverage: We loop from -abs(a) to abs(a) (and same for c) to include every possible positive and negative factor.
  • Clean output formatting: The code automatically adjusts signs so you get readable results like (x - 2)(x - 3) instead of messy ones like (1x + -2)(1x + -3).
  • Robust sign logic: It handles all combinations of positive/negative coefficients, including edge cases like x² - 1 (outputs (x - 1)(x + 1)) or 2x² + 3x - 2 (outputs (2x - 1)(x + 2)).

Test it out with those tricky negative-coefficient equations—this should work perfectly now!

内容的提问来源于stack exchange,提问作者daviddevz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:10:13