MOOC清华《程序设计基础》第2章第4题:解方程2

题目描述

请编写程序计算输出一元二次多项式 ax^2 + bx + c = 0 的两个根中较大的那一个,多项式的三个系数从键盘输入,保证均为正整数且一定有实根存在(b^2-4ac>=0)


输入描述

a,b,c均为整型,且以空格隔开

输出描述

输出较大根(两根相同输出该值即可),保留小数点后5位

样例输入

1 2 1
样例输出

-1.00000

//************************
//*程序名:解方程(2) 
//*功 能:解方程(2) 
//*编制人:刘聪
//*时 间:2017年1月30日
//************************ 
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
	float a=0.0,b=0.0,c=0.0,x1=0.0,x2=0.0,x=0.0;
	cin>>a>>b>>c;
	x1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
	x2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
	if(x1>=x2)x=x1;
	else x=x2;
	cout<<fixed<<setprecision(5)<<x<<endl;
	return 0;
}





posted on 2017-10-05 18:17  sunshineman1986  阅读(150)  评论(0编辑  收藏  举报

导航