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

### 2410 - Area of Squares

"Every problem has a simple, fast and wrong solution" OJ's Main Law. EduardoQuintana
Posts: 14
Joined: 7 years ago
Gender: ### Re: 2410 - Area of Squares

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

ymondelo20
Posts: 1968
Joined: 7 years ago
Location: Universidad de las Ciencias Informáticas
Gender: Contact:

### Re: 2410 - Area of Squares

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: 115
Joined: 5 years ago
Location: Camagüey - Cuba
Gender: Contact: ### Re: 2410 - Area of Squares

muy buena explicacion, gracias por eso
teruel

DiMas
Posts: 1
Joined: 2 years ago
Gender: ### Re: 2410 - Area of Squares

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: 115
Joined: 5 years ago
Location: Camagüey - Cuba
Gender: Contact: ### Re: 2410 - Area of Squares

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
``const double eps=1e-6;``