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.
Post Reply
User avatar
ymondelo20
Posts: 1968
Joined: 7 years ago
Location: Universidad de las Ciencias Informáticas
Gender: None specified
Contact:

2410 - Area of Squares

Post by ymondelo20 » 5 years ago



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

User avatar
EduardoQuintana
Posts: 14
Joined: 7 years ago
Location: Universidad de Cienfuegos (UCf)
Gender: None specified

Re: 2410 - Area of Squares

Post by EduardoQuintana » 5 years ago

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
Posts: 1968
Joined: 7 years ago
Location: Universidad de las Ciencias Informáticas
Gender: None specified
Contact:

Re: 2410 - Area of Squares

Post by ymondelo20 » 5 years ago

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: 4 years ago
Location: Camagüey - Cuba
Gender: Male
Contact:
Cuba

Re: 2410 - Area of Squares

Post by HaZard » 4 years ago

muy buena explicacion, gracias por eso
teruel

DiMas
Posts: 1
Joined: 2 years ago
Gender: Male

Re: 2410 - Area of Squares

Post by DiMas » 2 years ago

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: 4 years ago
Location: Camagüey - Cuba
Gender: Male
Contact:
Cuba

Re: 2410 - Area of Squares

Post by HaZard » 2 years ago

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

Post Reply

Return to “Problem set”