2410 - Area of Squares

Discussion around the problems of the COJ.
Forum rules
Remember that posting AC code is not allowed here. If you are going to ask a question or to post a solution, describe your algorithm instead. Posting AC code will be penalized.
User avatar
ymondelo20
COJ Administrator
Posts: 1968
Joined: Sun Nov 13, 2011 12:32 pm
Location: Universidad de las Ciencias Informáticas
Gender: None specified
Contact:

2410 - Area of Squares

Postby ymondelo20 » Sat May 25, 2013 6:14 pm



"Every problem has a simple, fast and wrong solution" OJ's Main Law. ;)

User avatar
EduardoQuintana
Posts: 14
Joined: Fri Nov 18, 2011 9:05 am
Location: Universidad de Cienfuegos (UCf)
Gender: None specified

Re: 2410 - Area of Squares

Postby EduardoQuintana » Thu Jun 20, 2013 11:56 pm

Basic geometry knowledge is necessary to solve this.
The length of the edge of the biggest square is given (A1). The length of the next square's edge (A2) can be easily found using Pitágora's relation:
A2=sqrt( (A1/2)^2 + (A1/2)^2 )=A1/sqrt(2).
Repeating the same reasoning for further minor squares inscribed, it is found that the length of the i_th square's edge (Ai) can be computed as:
Ai=A1/sqrt(2^(i-1)).
The area of the ith square is clearly the length of side squared:
Area_i = Ai^2 = A1^2/2^(i-1)

Pretty simple.

For avoiding machine precision errors, you should use double instead of float variables.
Further machine precision errors can be eluded by adding a small amount to your result, example:

Code: Select all

const double eps=1e-6;


This will deal with tricky cases such as:
1/8=0.125000, rounded to %.2lf -> 0.13
for which some compilers would return instead 1/8=0.124999, rounded to %.2lf -> 0.12 == Wrong Answer
L@grang3

User avatar
ymondelo20
COJ Administrator
Posts: 1968
Joined: Sun Nov 13, 2011 12:32 pm
Location: Universidad de las Ciencias Informáticas
Gender: None specified
Contact:

Re: 2410 - Area of Squares

Postby ymondelo20 » Tue Jun 25, 2013 11:12 am

A easy problem, with one tricky detail.
Good explanation.
Regards
"Every problem has a simple, fast and wrong solution" OJ's Main Law. ;)

HaZard
Posts: 113
Joined: Sun Feb 09, 2014 9:43 am
Location: Camagüey - Cuba
Gender: Male
Contact:

Re: 2410 - Area of Squares

Postby HaZard » Fri Sep 26, 2014 5:08 pm

muy buena explicacion, gracias por eso
teruel

DiMas
Posts: 1
Joined: Fri Oct 28, 2016 2:10 pm
Gender: Male

Re: 2410 - Area of Squares

Postby DiMas » Fri Nov 11, 2016 5:57 pm

Hola a todos, comprendo perfectamente la formula dada en el post que explica la solucion, de hecho, habia enviado el resultado antes de consultar aqui, pero, dado el bajo porcentaje de aceptados decidi mirar y me di cuenta de que llego al mismo resultado y obtengo wA, podrian por favor decirme que pasa.
Muchas Gracias.Atentamente
Dimas

HaZard
Posts: 113
Joined: Sun Feb 09, 2014 9:43 am
Location: Camagüey - Cuba
Gender: Male
Contact:

Re: 2410 - Area of Squares

Postby HaZard » Mon Nov 14, 2016 11:52 am

DiMas wrote:Hola a todos, comprendo perfectamente la formula dada en el post que explica la solucion, de hecho, habia enviado el resultado antes de consultar aqui, pero, dado el bajo porcentaje de aceptados decidi mirar y me di cuenta de que llego al mismo resultado y obtengo wA, podrian por favor decirme que pasa.
Muchas Gracias.Atentamente
Dimas


En el post de quintana viene bien explicado, leelo cuidadosamente, Saludos

EduardoQuintana wrote:For avoiding machine precision errors, you should use double instead of float variables.
Further machine precision errors can be eluded by adding a small amount to your result, example:

Code: Select all

const double eps=1e-6;


This will deal with tricky cases such as:
1/8=0.125000, rounded to %.2lf -> 0.13
for which some compilers would return instead 1/8=0.124999, rounded to %.2lf -> 0.12 == Wrong Answer
teruel


Return to “Problem set”

Who is online

Users browsing this forum: No registered users and 1 guest