URI Online Judge | 1036 Bhaskara's Formula (Solution)
Problem Link:
https://www.urionlinejudge.com.br/judge/en/problems/view/1036
Solution:
https://www.urionlinejudge.com.br/judge/en/problems/view/1036
URI Online Judge | 1036
Bhaskara's Formula
Adapted by Neilor Tonin, URI Brazil
Timelimit: 1
Read 3 floating-point numbers. After, print the roots of bhaskara’s formula. If was impossible to calculate the roots because a division by zero or a square root of a negative number, presents the message “Impossivel calcular”.
Input
Read 3 floating-point numbers A, B and C.
Output
Print the result with 5 digits after the decimal point or the message if it is impossible to calculate.
Sample Inputs | Sample Outputs |
10.0 20.1 5.1 | R1 = -0.29788 R2 = -1.71212 |
0.0 20.0 5.0 | Impossivel calcular |
10.3 203.0 5.0 | R1 = -0.02466 R2 = -19.68408 |
10.0 3.0 5.0 | Impossivel calcular |
#include<stdio.h>
#include<math.h>
int
main()
{
double
a,b,c,r1,r2,del;
scanf
(
"%lf %lf %lf"
,&a,&b,&c);
del=(b*b)-(4*a*c);
r1=(-b+
sqrt
(del))/(2*a);
r2=(-b-
sqrt
(del))/(2*a);
if
(a!=0 && del>0)
{
printf
(
"R1 = %.5lf\nR2 = %.5lf\n"
,r1,r2);
}
else
printf
(
"Impossivel calcular\n"
);
return
0;
}
This comment has been removed by the author.
ReplyDelete#include
ReplyDelete#include
int main()
{
double a,b,c;
scanf("%lf",&a);
scanf("%lf",&b);
scanf("%lf",&c);
double postive_root,negative_root;
double Z;
Z = sqrt( pow(b,2) - (4 * a * c ) );
postive_root = (-b + Z)/(2*a);
negative_root = (-b - Z)/(2*a);
if( a == 0 || Z<0 )
{
printf("Impossivel calcular\n");
}
else
{
printf("R1 =%.5lf\n",postive_root);
printf("R2 =%.5lf\n",negative_root);
}
return 0;
}
//why doesn't this work
#include
ReplyDelete#include
#include
using namespace std;
int main()
{
double a , b , c ,r1 , r2 , sq ;
cin >> a >> b >> c ;
sq = sqrt((b*b) - (4*a*c));
r1 = (-b+sq)/(2*a);
r2 = (-b-sq)/(2*a);
if (a!=0 && sq >0)
{
cout << "R1 = "<<fixed<<setprecision(5)<<r1<<endl;
cout << "R2 = "<<fixed<<setprecision(5)<<r2<<endl;
}
else
cout<< "Impossivel calcular" << endl;
return 0;
}
#include
ReplyDelete#include
using namespace std;
int main()
{
double a,b,c,r1,r2,d;
cin>>a>>b>>c;
d=pow(b,2)-4*a*c;
r1=(-b+sqrt(pow(b,2)-4*a*c))/(2*a);
r2=(-b-sqrt(pow(b,2)-4*a*c))/(2*a);
if(d>0&&a!=0)
{
cout<<"R1 = "<<fixed<<setprecision(5)<<r1<<"\n"<<"R2 = "<<fixed<<setprecision(5)<<r2<<"\n";
}
else
{
cout<<"Impossivel calcular/n";
}
return 0;
}
A,B,C=map(float,input().strip().split())
ReplyDeleteif(A==0):
print("Impossivel calcular")
elif ((B**2)-4*A*C)<0:
print("Impossivel calcular")
else:
formula1=float(((-B+((B**2)-4*A*C)**(1/2))/(2*A)))
formula2=float((-B-((B**2)-4*A*C)**(1/2))/(2*A))
print("R1 = %.5f"%formula1)
print("R2 = %.5f"%formula2)
Here's the solution in Python if you want it in C++ i'll do it ;)