18:点和正方形的关系
#include <iostream>
using namespace std;
int main()
{
int a,b;
cin >> a >> b;
-1<=a&&a<=1&&-1<=b&&b<=1 ? cout << "yes" : cout << "no";
}
19:简单计算器
#include <iostream>
using namespace std;
int main()
{
int a,b;
char c;
cin >> a >> b >> c;
if(c=='+')
cout << a+b;
if(c=='-')
cout << a-b;
if(c=='*')
cout << a*b;
if(c=='/')
if(b==0)
cout << "Divided by zero!";
else
cout << a/b;
if(c!='+'&&c!='-'&&c!='*'&&c!='/')
cout << "Invalid operator!";
}
嘛。。。不加花括号好像还挺好用的,这么说来只要工整一点让别人看懂就行了嘛?
20:求一元二次方程的根
啥啊,啥是虚根啊?不行,这超过我的智力范围了。。。。
一个半小时后,我终于做出来了
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
double a,b,c;
cin >> a >> b >>c;
cout << fixed;
double x1 =(-b + sqrt(b*b-4*a*c))/(2*a);
double x2 =(-b - sqrt(b*b-4*a*c))/(2*a);
double t =(-b)/(2*a);
double f =sqrt(4*a*c-b*b)/(2*a);
if(t==-0)
t=0;
if(b*b ==4*a*c)
cout << "x1=x2=" << setprecision(5)<< x1;
if(b*b>4*a*c)
cout << "x1=" << setprecision(5)<< x1 << ";x2="<< setprecision(5) << x2;
if(b*b<4*a*c)
cout << "x1="<< setprecision(5) <<t << "+" << f << "i;"
<< "x2="<< setprecision(5) << t << "-" << f << "i";
}
题目的分数比为1:4:5,别问我怎么知道的。话说为什么打印的时候会有-0这个东西啊,现在整个页面全是我错误的答案。。。。