求方程的根
时间: 1ms 内存:128M
描述:
求方程 的根,用三个函数分别求当b^2-4ac大于0、等于0、和小于0时的根,并输出结果。从主函数输入a、b、c的值。
输入:
a b c
输出:
x1=? x2=?
示例输入:
4 1 1
示例输出:
x1=-0.125+0.484i x2=-0.125-0.484i
提示:
参考答案(内存最优[0]):
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
float a,b,c,q;
float shigen(float a1,b1,q1)
{
float x1,x2;
x1=(-b+q1)/2*a1;
x2=(-b-q1)/2*a1;
cout<<"x1="<<x1<<" "<<"x2="<<x2<<endl;
return(x1,x2);
}
float denggen(float a1,b1)
{
float x;
x=-b/2*a1;
cout<<"x="<<x<<endl;
return(x);
}
float xugen(float a1,b1,q1)
{
q1=sqrt(q1/(-1))/(2*a1);
b1=(-b1)/(2*a1);
cout<<setiosflags(ios::fixed)<<setprecision(3)<<"x1="<<b1<<"+"<<q1<<"i"<<" "<<"x2="<<b1<<"-"<<q1<<"i"<<endl;
return(b1,q1);
}
int main()
{
float a,b,c,q;
void shigen(float,float,float);
void denggen(float ,float );
void xugen(float ,float ,float);
cin>>a>>b>>c;
q=b*b-4*a*c;
if(q>0) shigen(a,b,q);
else if(q==0) denggen(a,b);
else xugen(a,b,q);
return 0;
}
参考答案(时间最优[0]):
#include <iostream>
using namespace std;
int main()
{
int m;
cin>>m;
if((m==2 || m%2!=0) && m>1)
cout<<"prime";
else
cout<<"not prime";
return 0;
}
题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。