标签:pac ble fine ios iostream namespace name bsp 次方
问题描述
求一元二次方程ax2+bx+c=0(a≠0)的根。如果a=0,输出:error;如果判别式小于0,输出:No real roots.;如果判别式等于0,输出:x1=x2=xxxxxx;如果判别式大于0,输出:x1=xxxxxx,x2=xxxxxx。
输入格式三个用空格分隔的双精度数输出格式按上述要求输出根或error等样例输入1 3 1样例输出x1=-0.381966,x2=-2.618034提示
输入:1 2 1;输出:x1=x2=-1.000000
输入:1 1 1;输出:No real roots.
输入:0 1 1;输出:error
#include <stdio.h> #include <iostream> #include <math.h> #include <string.h> #define d 1e-7 using namespace std; int main() { double a,b,c; scanf("%lf %lf %lf",&a,&b,&c); double k,x1,x2; k = (b*b - 4.0*a*c); if(a==0) printf("error"); else { if(k > 0 ) { x1 = (-b + sqrt(k))/(2.0*a); x2 = (-b - sqrt(k))/(2.0*a); if(x1<x2) printf("x1=%lf,x2=%lf",x2,x1); else printf("x1=%lf,x2=%lf",x1,x2); } else if(k==0) { x1 = -b/(2.0*a); printf("x1=x2=%lf",x1); } else printf("No real roots."); } return 0; }
标签:pac ble fine ios iostream namespace name bsp 次方
原文地址:http://www.cnblogs.com/fzuhyj/p/8007561.html