Code: Select all
while(l + EPS < r) { ...}
Code: Select all
if(abs(val) <= EPS2) { res = mid ; }
Moderator: Board moderators
Code: Select all
while(l + EPS < r) { ...}
Code: Select all
if(abs(val) <= EPS2) { res = mid ; }
Code: Select all
#include<stdio.h>
#include<math.h>
float fx(float x,int p,int q,int r,int s,int t,int u){
float eq;
eq=p*exp(-(x)) + q*sin((x))+r*cos((x))+s*tan((x))+t*pow((x),2)+u;
return eq;
}
int main(){
int p,q,r,s,t,u;
float st,ed,mid,fx_st,fx_end,fx_mid,fx_dif;
while(scanf("%d %d %d %d %d %d",&p,&q,&r,&s,&t,&u)==6){
st=0;
ed=1;
if((p>=0 && p<=20)&&(r>=0 && r<=20)&&(q>=-20 && q<=0)&&(s>=-20 && s<=0)&&(t>=-20 && t<=0)){
while(1){
float abs;
mid=(st+ed)/2;
fx_st=fx(st,p,q,r,s,t,u);
fx_end=fx(ed,p,q,r,s,t,u);
fx_mid=fx(mid,p,q,r,s,t,u);
if((fx_end-fx_st)<0){
abs=-1*(fx_end-fx_st);
}
else
abs=(fx_end-fx_st);
if(fx_st<.00001 && fx_st>=0){
printf("%.4f\n",st);
break;
}
else if(fx_end<.00001 && fx_end>=0 ){
printf("%.4f\n",ed);
break;
}
else if(abs<.00001 )
{
printf("%.4f\n",mid);
break;
}
if(fx_st<0 && fx_end>=0 ){
if(fx_mid<0)
st=mid;
else
ed=mid;
}
else if(fx_end<0 && fx_st>=0){
if(fx_mid<0)
ed=mid;
else
st=mid;
}
else{
puts("No solution");
break;
}
}
}
else
puts("No solution");
}
return 0;
}
Code: Select all
#include <iostream>
#include <cmath>
using namespace std;
int main () {
short p, q, r, s, t, u;
while ( cin >> p >> q >> r >> s >> t >> u ) {
double temp, bef = 0.0, aft = 0.0;
int z;
bool sol_found = false;
for ( int x = 0; x <= 10000; x++ ) {
temp = double(x) / 10000;
double P = round( p * exp( -temp ) * 10000 ) / 10000;
double Q = round( q * sin( temp ) * 10000 ) / 10000;
double R = round( r * cos( temp ) * 10000 ) / 10000;
double S = round( s * tan( temp ) * 10000 ) / 10000;
double T = round( t * temp * temp * 10000 ) / 10000;
double U = ( double ) u;
aft = P + Q + R + S + T + U;
if ( x == 0 ) bef = aft;
if ( abs( 0 - aft ) > abs( 0 - bef ) ) {
sol_found = true;
z = x - 1;
break;
}
else {
bef = aft;
}
}
if ( sol_found ) {
string res = to_string( z );
switch ( res.length() ){
case 1 :
if ( z == 0 ) cout << "0.0000" << endl;
else cout << "0.000" + res << endl;
break;
case 2 :
cout << "0.00" + res << endl;
break;
case 3 :
cout << "0.0" + res << endl;
break;
case 4 :
if ( z == 10000 ) cout << "1.0000" << endl;
else cout << "0." + res << endl;
break;
}
}
else cout << "No solution" << endl;
}
return 0;
}