- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以我编写了一个 python 代码,使用二次公式求解 x。除了迹象,一切最终都会成功。例如,如果你想因式分解 x^2 + 10x + 25,当答案应该是 5、5 时,我的代码输出 -5、-5。
def quadratic_formula():
a = int(input("a = "))
b = int(input("b = "))
c = int(input("c = "))
bsq = b * b
fourac = 4 * a * c
sqrt = (bsq - fourac) ** (.5)
oppb = -b
numerator_add = (oppb) + (sqrt)
numerator_sub = (oppb) - (sqrt)
twoa = 2 * a
addition_answer = (numerator_add) / (twoa)
subtraction_answer = (numerator_sub) / (twoa)
print(addition_answer)
print(subtraction_answer)
最佳答案
您的解决方案很好,让我们使用 sympy 来证明它:
>>> (x**2+10*x+25).subs(x,-5)
0
如您所见,-5 是根之一,而 5
>>> (x**2+10*x+25).subs(x,5)
100
不是,现在......如果你扩展你的 2 个根 [-5,-5] 就像:
>>> ((x+5)*(x+5)).expand()
x**2 + 10*x + 25
你可以看到结果匹配。
其实你也可以通过显示二次方程来确认根是正确的:
我强烈建议您查看 The Quadratic Formula 的概念等一切都清楚了就回到编码上
关于python - 二次方程式混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39338450/
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
我求解 jQuery 二次方程的代码有什么问题? a = parseFloat($('#a').val()); b = parseFloat($('#b').val()); c = parseFloa
我应该在 matlab 代码中加入什么条件才能使用这些公式得到二次方程的精确解: x1=(-2*c)/(b+sqrt(b^2-4*a*c)) x2=(-2*c)/(b-sqrt(b^2-4*a*c))
我正在做一项学校作业。我应该实现一个类并提供方法 getSolution1 和 getSolution2。但是,我的代码有 2 个我无法弄清楚的问题。 问题 #1 在这一行: solution1= (
如果这个问题很简单,我很抱歉,但我是 C++ 的新手。我正在设计一个使用二次公式计算 2 个根的程序。但是,当我的判别式为负数时,我的程序不起作用。 #define _USE_MATH_DEFINES
我对 Javascript 不太熟悉。我希望有人能简单地解释一下编辑以下代码的过程。 this.hideNextButton(); this.hidePreviousButton(); var tha
我是一名优秀的程序员,十分优秀!