4 条题解

  • 1
    @ 2025-10-23 0:13:23

    数据改了 debug半天发现是a=0时变为线性关系也需要输出两个小数而不是一个,,, 下面的题解应该都是数据没改的时候的 用不了了 我重写一个

    #include<iostream>
    #include<stdio.h>
    #include<string>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    double a,b,c;
    int main()
    {
    	scanf("%lf %lf %lf",&a,&b,&c);
    	if(a==0){
    		if(b==0){
    			printf("no solution");
    		}
    		else printf("%.6lf %.6lf",-(c/b),-(c/b));	
    	}
    	else {
    		double dd=b*b-4*a*c;
    		if(dd<0){
    			printf("no solution");
    		}
    		else{
    			double x1=((-b)-sqrt(dd))/(2*a);
    			double x2=((-b)+sqrt(dd))/(2*a);
    			if(x1<x2)	printf("%.6lf %.6lf",x1,x2);
    			else printf("%.6lf %.6lf",x2,x1);			
    		}
    	}
    	return 0;
    }
    
    • 1
      @ 2025-10-2 23:00:47
      #include <iostream>
      #include <cmath>
      #include <cstdio>
      using namespace std;
      
      int main() 
      {
          double a, b, c;
          cin >> a >> b >> c;
          
          if (a == 0) 
          {
              // 处理一次方程情况
              if (b == 0) 
              {
                  // 无解
                  cout << "no solution" << endl;
              } 
              else 
              {
                  double ans = -c / b;
                  // 输出两个相同的解,以满足题目输出格式要求
                  printf("%.6f", ans);
              }
          } 
          else 
          {
              double delta = b * b - 4 * a * c;
              if (delta < 0) 
              {
                  cout << "no solution" << endl;
              } 
              else 
              {
                  double ans1 = (-b - sqrt(delta)) / (2 * a);
                  double ans2 = (-b + sqrt(delta)) / (2 * a);
                  if (ans1 > ans2) 
                  {
                      printf("%.6f %.6f\n", ans2, ans1);
                  } 
                  else 
                  {
                      printf("%.6f %.6f\n", ans1, ans2);
                  }
              }
          }
          
          return 0;
      }
      
      • 0
        @ 2025-10-2 18:50:28

        有没有人和我一样,忘记对a==0的考虑,从初中错到大学::>_<::

        不仅要考虑a,还有b,c的等不等于0的情况。

        #include<stdio.h>
        #include<string.h>
        #include<stdlib.h> 
        #include<math.h>
        
        int main(){
        	int a,b,c;
        	scanf("%d %d %d",&a,&b,&c);
        	double d;
        	d=b*b*1.0-4*a*c*1.0;
        	if(a==0){
        		if(b==0){
        			printf("no solution");
        		}
        		else{
        		
        		printf("%.6lf",(-c*1.0)/b);
        		return 0;}
        	}
        
        	if(d<0){
        		printf("no solution");
        		return 0;
        	}
        	if(d>=0){
        		double x1;
        		double x2;
        		x1=(-b+sqrt(d))/(2*a);
        		x2=(-b-sqrt(d))/(2*a);
        		if(a>0) printf("%.6lf %.6lf",x2,x1);
        		if(a<0) printf("%.6lf %.6lf",x1,x2);
        	}
            return 0;
        }
        
        • 0
          @ 2024-10-7 19:11:40
          #include <stdio.h>
          #include <math.h>
          int main()
          {
          	int a = 0;
          	int b = 0;
          	int c = 0;
          	double d = 0.0;
          	double f = 0.0;
          	double r1 = 0.0;
          	double r2 = 0.0;
          	scanf("%d %d %d", &a, &b, &c);
          	d = pow(b, 2) - 4 * a * c;
          	if (a == 0)
          	{
          		if (b == 0)
          		{
          			if(b==0&&c!=0)
          			printf("no solution");
          			if(b==0&&c==0)
          			printf("%.6f",(float)c);
          		}
          		else printf("%.6f", (float)-1.0*c / b);
          	}
          	else
          	{
          		if (d < 0)
          			printf("no solution");
          		if (d > 0)
          		{
          			f = sqrt(d);
          			r1 = 1.0*(-b - f) / (2 * a);
          			r2 = 1.0*(-b + f) / (2 * a);
          			if (r1 > r2)
          			{
          				printf("%.6f %.6f", (float)r2, (float)r1) ;
          			}
          			else if (r1 < r2)
          			{
          				printf("%.6f %.6f", (float)r1, (float)r2) ;
          			}
          		}
          		if (d == 0)
          		{
          			printf("%.6f %.6f",-1.0*b/(2*a),-1.0*b/(2*a));
          		}
          	}
          	return 0;
          }
          

          *注: 1.如果出现"-inf"的情况,这个代表该数为负无穷大,很有可能出现了x/0的情况 2.注意"有解"的情况只需输出一次 3.注意考虑各变量=0的情况

          • 1

          信息

          ID
          22
          时间
          1000ms
          内存
          512MiB
          难度
          3
          标签
          递交数
          1789
          已通过
          39
          上传者