10341 - Solve It

All about problems in Volume 103. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

tiendaotd
New poster
Posts: 12
Joined: Mon Jan 14, 2013 12:21 pm

Re: 10341 - Solve It

Post by tiendaotd »

I solved this problems with Binary Search and two EPS value ( :o ) . One EPS = 1e-7 for comparing the low and high value of binary search like

Code: Select all

 while(l + EPS < r) { ...} 
, and one EPS=1e-5 for checking the value of f function is zero like that :

Code: Select all

 if(abs(val) <= EPS2) { res = mid ; } 
I don't really know why this trick worked , hope someone could explain it. Thanks in advanced.
MauriWilde
New poster
Posts: 14
Joined: Sun Jan 20, 2013 1:58 am

Re: 10341 - Solve It

Post by MauriWilde »

Could someone please explain me this?

When I use: puts("No solution"); my code give me AC.

But when I use cout << "No solution" << endl; (with and without endl or \n) my SAME code gives me Wrong Answer...

:-? :-? :-? :-?
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10341 - Solve It

Post by brianfry713 »

Post your code.
Check input and AC output for thousands of problems on uDebug!
mdminhaj329
New poster
Posts: 2
Joined: Thu Apr 23, 2015 7:25 am

Re: 10341 - Solve It

Post by mdminhaj329 »

why wa ,,,please help......

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;

}
infinia249
New poster
Posts: 3
Joined: Fri Oct 02, 2015 8:15 am

Re: 10341 - Solve It

Post by infinia249 »

Wrong Answer? Already tested this using inputs from uDebug Random Inputs, all clear. Where are the mistakes on this code? (my algorithm is somehow a brute-forced one)

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;
}
Post Reply

Return to “Volume 103 (10300-10399)”