为jun木而生


私信TA

用户名:seventh

访问量:13676

签 名:

chongchongchong!!!

等  级
排  名 738
经  验 3731
参赛次数 2
文章发表 21
年  龄 0
在职情况 学生
学  校 湖南警察学院
专  业 计科

  自我简介:

emmmmmmm

解题思路与注意事项:


             1.建立三个子函数,分别对应b*b-4*a*c大于等于小于0三种情况

             2.注意控制格式,保留三位小数输出"%.3lf"

             3.math里面的sqrt()开平方函数,参数为正数。

              所以当b*b-4*a*c为负数时,应在参数前加负号已确认为正数



参考代码:

#include<stdio.h>
#include<math.h>
double q,p;
double fun1(double a,double b,double c,double s)
{
   p=-b/(2*a);
   q=(sqrt(s))/(2*a);
   printf("x1=%.3lf x2=%.3lf\n",p+q,p-q);
   return 0;
}
double fun2(double a,double b,double c)
{
   p=-b/(2*a);
   printf("x1=%.3lf x2=%.3lf\n",p,p);
   return 0;
}
double fun3(double a,double b,double c,double s)
{
   p=-b/(2*a);
   q=sqrt(-s)/(2*a);
   printf("x1=%.3lf+%.3lfi x2=%.3lf-%.3lfi\n",p,q,p,q);
   return 0;
}
int main()
{
   double a,b,c,s;
   scanf("%lf%lf%lf",&a,&b,&c);
   s=b*b-4*a*c;
   if(s>0)fun1(a,b,c,s);
   else if(s==0)fun2(a,b,c);
   else fun3(a,b,c,s);
   return 0;
}

感觉代码对你有用的话,留下你的赞赞吧(#^.^#)

 

0.0分

24 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区

#include<stdio.h>
#include<math.h>
int main()
{
	float a,b,c,delt,x1,x2;
	scanf("%f%f%f",&a,&b,&c);
	delt=b*b-4*a*c;
    if (delt==0) 
	{ 
	x1=(-b)/(2*a); 
	x1=(-b)/(2*a); 
	printf("x1=%f\n",x1); 
	} 
	if (delt>0) 
	{ 
	x1=(-b+sqrt(delt))/(2*a); 
	x2=(-b-sqrt(delt))/(2*a); 
	printf("x1=%f,x2=%f\n",x1,x2);
	}
	return 0;
}
求问大佬,哪里错了
2020-02-21 14:38:29
#include<stdio.h>
#include<math.h>
double a,b,c,p,q;
int solve(double a,double b,double c)
{
    if(b*b-4*a*c>0)
    {
        p=-b/(2*a);
        q=(sqrt(b*b-4*a*c))/(2*a);
        printf("x1=%.3lf x2=%.3lf\n",p+q,p-q);
    }
    else if(b*b-4*a*c==0)
    {
        p=-b/(2*a);
        printf("x1=%.3lf x2=%.3lf\n",p,p);
    }
    else if(b*b-4*a*c<0)
    {
        p=-b/(2*a);
        q=(sqrt(4*a*c-b*b))/(2*a);
        printf("x1=%.3lf+%.3lfi x2=%.3lf-%.3lfi",p,q,p,q);
    }
}
int main()
{
    scanf("%ld %lf %lf",&a,&b,&c);
    solve(a,b,c);
}
2019-12-03 20:13:41
为什么我不能写的不正确,虚数i感觉没什么不一样啊
大神看看哪里错了
#include<stdio.h>
#include<math.h>
int main()
{
	int a,b,c,m;
	double x1,x2;
	scanf("%d%d%d",&a,&b,&c);
	m=b*b-4*a*c;
	if(m>=0)
	{
		x1=(-b+sqrt(m))/(2.0*a);
		x2=(-b-sqrt(m))/(2.0*a);
		printf("x1=%.3lf x2=%.3lf",x1,x2); 
	}
	else
	{
		m=-m;
		x1=(-b)/(2.0*a);
		double x11=sqrt(m)/(2.0*a);
		x2=(-b)/(2.0*a);
		double x22=sqrt(m)/(-2.0*a);
		printf("x1=%.3lf+%.3lfi x2=%.3lf+%.3lfi",x1,x11,x2,x22);
	}
	
	return 0;
}
2019-11-30 20:37:20
  • «
  • 1
  • »