【问题标题】:Solving Quadratic Equation求解二次方程
【发布时间】:2013-03-02 03:53:06
【问题描述】:

我的程序似乎没有给我正确的解决方案。有时会,有时不会。我找不到我的错误。有什么建议吗?

import math

a,b,c = input("Enter the coefficients of a, b and c separated by commas: ")

d = b**2-4*a*c # discriminant

if d < 0:
    print "This equation has no real solution"
elif d == 0:
    x = (-b+math.sqrt(b**2-4*a*c))/2*a
    print "This equation has one solutions: ", x
else:
    x1 = (-b+math.sqrt(b**2-4*a*c))/2*a
    x2 = (-b-math.sqrt(b**2-4*a*c))/2*a
    print "This equation has two solutions: ", x1, " and", x2

【问题讨论】:

    标签: python python-2.7


    【解决方案1】:

    这行导致问题:

    (-b+math.sqrt(b**2-4*a*c))/2*a
    

    x/2*a 被解释为(x/2)*a。你需要更多的括号:

    (-b + math.sqrt(b**2 - 4*a*c)) / (2 * a)
    

    另外,如果您已经存储了d,为什么不使用它呢?

    x = (-b + math.sqrt(d)) / (2 * a)
    

    【讨论】:

    • 哇...我花了一个多小时才弄清楚。非常感谢搅拌机。带有“d”变量的第二部分也很有帮助。
    【解决方案2】:

    给你,这应该每次都能给你正确的答案!

    a = int(input("Enter the coefficients of a: "))
    b = int(input("Enter the coefficients of b: "))
    c = int(input("Enter the coefficients of c: "))
    
    d = b**2-4*a*c # discriminant
    
    if d < 0:
        print ("This equation has no real solution")
    elif d == 0:
        x = (-b+math.sqrt(b**2-4*a*c))/2*a
        print ("This equation has one solutions: "), x
    else:
        x1 = (-b+math.sqrt((b**2)-(4*(a*c))))/(2*a)
        x2 = (-b-math.sqrt((b**2)-(4*(a*c))))/(2*a)
        print ("This equation has two solutions: ", x1, " or", x2)
    

    【讨论】:

    • 对于 d == 0,你应该把 2*a 放在括号里
    【解决方案3】:
    # syntaxis:2.7
    # solution for quadratic equation
    # a*x**2 + b*x + c = 0
    
    d = b**2-4*a*c # discriminant
    
    if d < 0:
        print 'No solutions'
    elif d == 0:
        x1 = -b / (2*a)
        print 'The sole solution is',x1
    else: # if d > 0
        x1 = (-b + math.sqrt(d)) / (2*a)
        x2 = (-b - math.sqrt(d)) / (2*a)
        print 'Solutions are',x1,'and',x2
    

    【讨论】:

      【解决方案4】:
      import math   
      a = int(input("Enter the coefficients of a: "))
      b = int(input("Enter the coefficients of b: "))
      c = int(input("Enter the coefficients of c: "))
      
      d = b**2-4*a*c # discriminant
      
      if d < 0:
          print ("This equation has no real solution")
      elif d == 0:
          x = (-b+math.sqrt(b**2-4*a*c))/2*a
          print (("This equation has one solutions: "), x)
      #add the extra () above or it does not show the answer just the text.
      else:
          x1 = (-b+math.sqrt((b**2)-(4*(a*c))))/(2*a)
          x2 = (-b-math.sqrt((b**2)-(4*(a*c))))/(2*a)
          print ("This equation has two solutions: ", x1, " or", x2)
      

      【讨论】:

        【解决方案5】:
        <code>
        import cmath
        import math
        print(" we are going to programming second grade equation in python")
        print(" a^2 x + b x + c =0")
        
        num1 = int(input(" enter A please : "))
        num2 = int(input(" enter B please : "))
        num3 = int(input(" enter c please : "))
        v = num2*num2 - 4 *num1 * num3
        print(v)
        if v < 0 :
            print("wrong values")
        else:
            print("root of delta =", v)
            k= math.sqrt(v)
        
        def two_sol(x,y) :
            x_f= (-y + v)/(4*x)
            x_s =(-y - v)/(4*x)
            return x_f , x_s
        
        def one_sol(x):
            x_f = (-y + v) / (4 * x)
        
        if v >0 :
            print("we have two solution :" ,two_sol(num1,num2)) 
        elif v == 0:
            print( "we have one solution :" , one_sol(y)) 
        else:
            print(" there is no solution !!")
        </code>
        

        【讨论】:

          【解决方案6】:

          接受复杂的根作为解决方案怎么样?

          import math
          
          # User inserting the values of a, b and c
          
          a = float(input("Insert coefficient a: "))
          b = float(input("Insert coefficient b: "))
          c = float(input("Insert coefficient c: "))
          
          discriminant = b**2 - 4 * a * c
          
          if discriminant >= 0:
              x_1=(-b+math.sqrt(discriminant))/2*a
              x_2=(-b-math.sqrt(discriminant))/2*a
          else:
              x_1= complex((-b/(2*a)),math.sqrt(-discriminant)/(2*a))
              x_2= complex((-b/(2*a)),-math.sqrt(-discriminant)/(2*a))
          
          if discriminant > 0:
              print("The function has two distinct real roots: ", x_1, " and ", x_2)
          elif discriminant == 0:
              print("The function has one double root: ", x_1)
          else:
              print("The function has two complex (conjugate) roots: ", x_1, " and ", x_2)
          

          【讨论】:

          • 你犯了同样的错误,犯 (2*a),孩子
          【解决方案7】:

          通过键盘输入

          a=float(input("enter the 1st number : "))
          b=float(input("enter the 2nd number : "))
          c=float(input("enter the 3rd number : "))
          

          计算判别式

          d = (b**2) - (4*a*c)
          

          可能的解决方案是

          sol_1 = (-b-(0.5**d))/(2*a)
          sol_2 = (-b+(0.5**d))/(2*a)
          

          打印结果

          print('The solution are  %0.f,%0.f'%(sol_1,sol_2))
          

          【讨论】:

            【解决方案8】:

            下面是求解二次方程的程序。

            例如:求解 x2 + 3x – 4 = 0

            这个二次方程恰好是因式:

            x2 + 3x – 4 = (x + 4)(x – 1) = 0

            我们已经知道解是 x = –4 和 x = 1。

                # import complex math module
                import cmath
            
                a = 1
                b = 5
                c = 6
            
                # To take coefficient input from the users
                # a = float(input('Enter a: '))
                # b = float(input('Enter b: '))
                # c = float(input('Enter c: '))
            
                # calculate the discriminant
                d = (b**2) - (4*a*c)
            
                # find two solutions
                sol1 = (-b-cmath.sqrt(d))/(2*a)
                sol2 = (-b+cmath.sqrt(d))/(2*a)
            
                print('The solution are {0} and {1}'.format(sol1,sol2))
            

            来源:Python Program to Solve Quadratic Equation

            【讨论】:

              【解决方案9】:

              单线求解二次方程

              from math import sqrt
              s = lambda a,b,c: {(-b-sqrt(d))/2*a,(-b+sqrt(d))/2*a} if (d:=b**2-4*a*c)>=0 else {}
              roots_set = s(int(input('a=')),int(input('b=')),int(input('c=')))
              print(roots_set,f'number of roots {len(roots_set)}')
              

              one line python solve quadratic equations video

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2017-04-24
                • 2018-08-05
                • 2016-08-06
                • 1970-01-01
                • 2022-06-11
                相关资源
                最近更新 更多