HUST 1541 Student’s question
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 }
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Yii框架的路由配置方法分析
- Android 购物车加减功能的实现代码
- Yii框架函数简单用法分析
- PHP读取XML文件的方法实例总结【DOMDocument及simplexml方法】
- 浅析PHP7 的垃圾回收机制
- android BottomSheetDialog新控件解析实现知乎评论列表效果(实例代码)
- Laravel框架实现的上传图片到七牛功能详解
- Flutter沉浸式状态栏/AppBar导航栏/仿咸鱼底部凸起导航栏效果
- Android集成zxing扫码框架功能
- Android 实现抖音小游戏潜艇大挑战的思路详解
- 修改Android Studio 的 Logcat 缓冲区大小操作
- Android自定义View验证码输入框
- PHP生成随机字符串实例代码(字母+数字)
- Laravel框架中缓存的使用方法分析
- android实现搜索功能并将搜索结果保存到SQLite中(实例代码)