游客 Signup | Login
中文 | En

1144 - C语言5.6

求方程式ax2+bx+c=0的根。

Input

三个用空格隔开的整数a、b、c。

Output

若a=0,则输出“not a quadratic”;否则若方程有两个相等实根,输出“two equal roots:”和相等实根的值;否则若方程有两个不等实根,输出“distinct real roots:”和两个不等实根,用空格隔开;否则若方程有两个共轭复根,输出“complex roots:”和两个共轭复根,用空格隔开。所有的实数输出请使用C语言的默认舍入方式保留4位小数,注意行尾输出换行。

Examples

Input

1 2 2

Output

complex roots:-1.0000+1.0000i -1.0000-1.0000i

Solution C

#include<stdio.h>
#include<math.h>
 
int main(){
  int a,b,c;
  int disc;
  double p1,p2,p3;
  scanf("%d %d %d",&a,&b,&c);
  if(a==0)
    printf("not a quadratic\n");
  else{
	    disc=b*b-4*a*c;
    p1=-b/(2.0*a);
	    p2=sqrt(disc)/(2.0*a);
    p3=sqrt(-disc)/(2.0*a);
	    if(disc<0)
		      printf("complex roots:%.4lf+%.4lfi %.4lf-%.4lfi\n",p1,p3,p1,p3);
	    else if(disc==0)
		      printf("two equal roots:%.4lf\n",-b/(2.0*a));
	    else if(disc>0)
		      printf("distinct real roots:%.4lf %.4lf\n",p1+p2,p1-p2);	  
  } 
  return 0;
}

Solution C++

#include <stdio.h>
#include <math.h>
int main() {
	int a, b, c, delta;
	double ans0, ans1, sqrt_delta, realpart, imagpart;
	scanf("%d %d %d", &a, &b, &c);
	/* 计算delta的值(b*b-4*a*c)并分类讨论 */
	delta = b * b - 4 * a * c;
	if (delta > 0) {
		sqrt_delta = sqrt(delta);
		ans0 = (-b - sqrt_delta) / (2.0 * a);
		ans1 = (-b + sqrt_delta) / (2.0 * a);
		printf("distinct real roots:%.4f %.4f\n", ans0, ans1);
	} else if (delta == 0) {
		ans0 = (-b) / (2.0 * a);
		printf("two equal roots:%.4f\n", ans0);
	} else {
		realpart = -b / (2.0 * a);
		imagpart = sqrt(-delta) / (2.0 * a);
		printf("complex roots:%.4f+%.4fi %.4f-%.4fi\n", realpart, imagpart, realpart, imagpart);
	}
	return 0;
}

Time Limit 1 second
Memory Limit 32 MB
Discuss Stats
上一题 下一题