Easy

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] 0.5 seconds (c)

  • [input] integer n

    Guaranteed constraints:
    1 ≤ n < 10^4.

  • [output] integer

    • The area of the n-interesting polygon.

[C] Syntax Tips

// Prints help message to the console
// Returns a string
char * helloWorld(char * name) {
    char * answer = malloc(strlen(name) + 8);
    printf("This prints to the console when you Run Tests");
    strcpy(answer, "Hello, ");
    strcat(answer, name);
    return answer;
}

더보기

Solution

int shapeArea(int n)
{
	int size=1;

	for(int i=1;i<n;i++)
		size+=i*4;

	return size;
}
728x90

'Codesignal' 카테고리의 다른 글

<Codesignal> matrixElementsSum  (0) 2020.04.05
<Codesignal> almostIncreasingSequence  (0) 2020.04.05
<Codesignal> adjacentElementsProduct  (0) 2020.04.05
<Codesignal> checkPalindrome  (0) 2020.04.05
<Codesignal> centuryFromYear  (0) 2020.04.05

+ Recent posts