3 条题解

  • 2
    @ 2026-7-28 17:00:24

    不喜勿喷!

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    using namespace std;
    int main()
    {
    	double a, b, c, x1, x2;
    	cin >> a >> b >> c;
    	if(a == 0 || b * b - 4 * a * c < 0)
    		cout << "No root.";
    	else
    	{
    		x1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
    		x2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
    		if(x1 == x2)
    			printf("x=%.2f",x1);
    		else
    		{
    			if(x1 > x2)
    				swap(x1,x2);
    			printf("x1=%.2f\nx2=%.2f",x1,x2);
    		}
    	}
    	
    	return 0;
    }
    
    
    • 1
      @ 2026-7-5 20:45:44

      匪肠的煎蛋:

      #include<bits/stdc++.h>
      using namespace std;
      
      int main(){
          double a,b,c;//没double(float/...)0分!
          cin>>a>>b>>c;
          if(a==0||b*b-4*a*c<0){
              cout<<"No root.";
              return 0;
          }
          double maxx=(-b+sqrt(b*b-4*a*c))/(2*a);
          double minn=(-b-sqrt(b*b-4*a*c))/(2*a);
          /*
          if(maxx<minn){
              int d=maxx;
              maxx=minn;
              minn=d;
          }
          */
          //因为所有测试点中,a、b、c均没有负数情况,所以没有这串代码也能侥幸AC😃😃😃
          if(maxx!=minn) printf("x1=%.2lf\nx2=%.2lf",maxx,minn);
                       //cout<<"x1="<<maxx<<"\nx2="<<minn;
          else printf("x=%.2lf",minn);//minn改成maxx也行
             //cout<<"x="<<minn(换成maxx也可以)
          return 0;
      }
      
      

      备注:\n可以换成endl

      最重要的一点:一元二次方程公式

      x=b±b24ac2ax = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}
      • 1
        @ 2023-9-2 16:20:24
        #include <iostream>
        #include <cmath>
        #include <iomanip>
        using namespace std;
        int main()
        {
           double a,b,c;
           cin>>a>>b>>c;
           double delta=b*b-4*a*c;
           if (delta>0)
           { 
              double x1=(-b+sqrt(delta))/(2*a);
              double x2=(-b-sqrt(delta))/(2*a);
              cout<<fixed<<setprecision(2);                    
              cout<<"x1="<<x1<<endl;
              cout<<"x2="<<x2<<endl;
           }
          else if (delta==0)
          {
             double x=-b/(2*a);
             cout<<fixed<<setprecision(2);
             cout<<"x="<<x<< endl;
          }
          else
          {
            cout<<"No root."<< endl;
          }
          return 0;
        }
        
        • 1

        信息

        ID
        1499
        时间
        1000ms
        内存
        256MiB
        难度
        4
        标签
        递交数
        38
        已通过
        18
        上传者