[Code Signal][js] shape Area
2342 ワード
🎆質問する
Below we will define an n-interesting polygon. Your task is to find the area of a polygon for a given n.
A 1-interesting polygon is just a square with a side of length 1. An n-interesting polygon is obtained by taking the n - 1-interesting polygon and appending 1-interesting polygons to its rim, side by side. You can see the 1-, 2-, 3- and 4-interesting polygons in the picture below.
Example
For n = 2, the output should be
shapeArea(n) = 5;
For n = 3, the output should be
shapeArea(n) = 13.
Input/Output
[execution time limit] 4 seconds (js)
[input] integer n
Guaranteed constraints:
1 ≤ n < 104.
[output] integer
The area of the n-interesting polygon.
🎆Solution
Below we will define an n-interesting polygon. Your task is to find the area of a polygon for a given n.
A 1-interesting polygon is just a square with a side of length 1. An n-interesting polygon is obtained by taking the n - 1-interesting polygon and appending 1-interesting polygons to its rim, side by side. You can see the 1-, 2-, 3- and 4-interesting polygons in the picture below.
Example
For n = 2, the output should be
shapeArea(n) = 5;
For n = 3, the output should be
shapeArea(n) = 13.
Input/Output
[execution time limit] 4 seconds (js)
[input] integer n
Guaranteed constraints:
1 ≤ n < 104.
[output] integer
The area of the n-interesting polygon.
🎆Solution
function shapeArea(n) {
let j = 1;
if(n === 1) return 1;
for(i = 2; i < n; i++) {
j = j + i;
}
return 1 + 4*j;
}
Reference
この問題について([Code Signal][js] shape Area), 我々は、より多くの情報をここで見つけました https://velog.io/@gygy/Code-Signaljs-shape-Areaテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol