2410 - Area of Squares
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.
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: 9 years ago
- Location: Universidad de las Ciencias Informáticas
- Gender:
- Contact:
- EduardoQuintana
- Posts: 14
- Joined: 9 years ago
- Location: Universidad de Cienfuegos (UCf)
- 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:
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
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;
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: 9 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
Good explanation.
Regards
"Every problem has a simple, fast and wrong solution" OJ's Main Law. 

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
Muchas Gracias.Atentamente
Dimas
Re: 2410 - Area of Squares
En el post de quintana viene bien explicado, leelo cuidadosamente, SaludosDiMas 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
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:
This will deal with tricky cases such as:Code: Select all
const double eps=1e-6;
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