HUST 1541 Student’s question

简介: 1541 - Student’s question 时间限制:1秒 内存限制:128兆 696 次提交 134 次通过 题目描述     YYis a student. He is tired of calculating the quadratic equation.

1541 - Student’s question

时间限制:1秒 内存限制:128兆

696 次提交 134 次通过
题目描述

    YYis a student. He is tired of calculating the quadratic equation. He wants you to help him to get the result of the quadratic equation. The quadratic equation’ format is as follows: ax^2+bx+c=0.

输入

       The first line contains a single positive integer N, indicating the number of datasets. The next N lines are n datasets. Every line contains three integers indicating integer numbers a,b,c (a≠0).

输出

       For every dataset you should output the result in a single line. If there are two same results, you should just output once. If there are two different results, you should output them separated by a space. Be sure the later is larger than the former. Output the result to 2 decimal places. If there is no solution, output “NO”.

样例输入
3
1 2 1
1 2 3
1 -9 6
样例输出
-1.00
NO
0.73 8.27

题目链接: http://acm.hust.edu.cn/problem/show/1541
分析:此题坑点很多!比赛中看到有人WA了11次都没过!原因在于要取double型而非int型,取double,直接AC,虽然弱弱也WA了3次才想到这一点!
大意就是要求解方程的根,常规做法就是先判断△=b*b-4*a*c的值,小于0无解,输出NO;等于0,一解;大于0,两解!但要注意的是两个解要从小到大进行有序输出!
而如何求解x1,x2,根据韦达定理得:x1+x2=-b/a,x1*x2=c/a去求解x1-x2=sqrt((x1+x2)*(x1+x2)-4*x1*x2),然后就可以求出x1,x2啦!还要记得保留两位小数哟!
下面给出AC代码:
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     double n,a,b,c;
 6     while(cin>>n)
 7     {
 8         while(n--)
 9         {
10             cin>>a>>b>>c;
11             if(a==0)break;
12             else
13             {
14             if(b*b-4*a*c>=0)
15             {
16                 double t1=(-b)/a;
17                 double t2=c/a;
18                 double t3=sqrt(t1*t1-4*t2);
19                 double x1=(t1+t3)/2;
20                 double x2=(t1-t3)/2;
21                 if(x1==x2) cout<<fixed<<setprecision(2)<<x1<<endl;
22                 else if(x1<x2)
23                 cout<<fixed<<setprecision(2)<<x1<<" "<<x2<<endl;
24                 else if(x1>x2)
25                     cout<<fixed<<setprecision(2)<<x2<<" "<<x1<<endl;
26             }
27             else cout<<"NO"<<endl;
28             }
29         }
30     }
31     return 0;
32 }

 

目录
相关文章
|
人工智能
Practical Skill Test——AT
题目描述 We have a grid with H rows and W columns. The square at the i-th row and the j-th column will be called Square (i,j). The integers from 1 through H×W are written throughout the grid, and the integer written in Square (i,j) is Ai,j.
81 0
PAT (Advanced Level) Practice - 1039 Course List for Student(25 分)
PAT (Advanced Level) Practice - 1039 Course List for Student(25 分)
74 0
|
SQL Java 中间件
SELECT * FROM GIRLS WHERE AGE BETWEEN 20 AND 24 ...
阿粉最近看到一张图,如上所示,原本只是一个搞笑的图,但是在阿粉看来这分明是个渣男啊!一句普通的 SQL 语句SELECT * FROM GIRLS WHERE AGE BETWEEN 20 AND 24 AND BOYFRIEND IS NULL,也有很多内涵! 什么?没看出来?来,阿粉带你品品。
SELECT * FROM GIRLS WHERE AGE BETWEEN 20 AND 24 ...
component set load logic - why coms_pcat_bob is accessed during product search
component set load logic - why coms_pcat_bob is accessed during product search
112 0
component set load logic - why coms_pcat_bob is accessed during product search
My Appointment - Belonging to me, Search by team, Search by group
My Appointment - Belonging to me, Search by team, Search by group
How China's Developers Are Defining The Information Age (Infographic 5)
China’s Developers: the technologies of tomorrow that are defining the information age
1600 0
How China's Developers Are Defining The Information Age (Infographic 5)
How China's Developers Are Defining The Information Age (Infographic 4)
China’s Developers: the essentials of today that are defining the information age
1453 0
How China's Developers Are Defining The Information Age (Infographic 4)
How China's Developers Are Defining The Information Age (Infographic 3)
China’s Developers: the trending technologies that are defining the information age
1552 0
How China's Developers Are Defining The Information Age (Infographic 3)
How China's Developers Are Defining The Information Age (Infographic 2)
China’s Developers: the tools that are defining the information age
1569 0
How China's Developers Are Defining The Information Age (Infographic 2)
How China's Developers Are Defining The Information Age (Infographic 1)
China's Developers at a Glance: 9 key takeaways from China's Developer Survey Report 2017
1844 0
How China's Developers Are Defining The Information Age (Infographic 1)