본문 바로가기

코드포스

[코드포스 Practice8] A. Alex and a Rhombus

While playing with geometric figures Alex has accidentally invented a concept of a n-th order rhombus in a cell grid.

 

기하학적인 도형으로 놀다가 Alex는 우연히 좌표계에서 n번째 순서의 마름모 개념을 발명합니다


A 1-st order rhombus is just a square 1×1 (i.e just a cell).

 

첫번째 순서의 마름모는 단지 하나의 네모입니다 (즉, 한 칸임)


A n-th order rhombus for all n≥2 one obtains from a n−1th order rhombus adding all cells which have a common side with it to it (look at the picture to understand it better).

 

n이 2 이상인 모든 n번째 마름모는 n-1번째 마름모의 모든 공통면을 포함합니다?? (더 잘 이해하기 위해서 아래 사진을 보세요)

Alex asks you to compute the number of cells in a n-th order rhombus.

 

Alex는 n번째 마름모의 세포의 수를 계산하라 했다.


Input
The first and only input line contains integer n (1≤n≤100) — order of a rhombus whose numbers of cells should be computed.

 

첫번째 줄에 단 하나만 정수 n을 둔다. (1이상 100이하)

n ㅡ 세포의 개수가 계산되어질 마름모의 순서 


Output
Print exactly one integer — the number of cells in a n-th order rhombus.

 

정수 하나만 출력하세용

- n번째 마름모에 있는 세포의 개수


 

 

 

이 문장만 보고 바로 그림으로 이해하려 했다. n=1, 2, 3으로 정사각형에 뾰족한거 튀어나온 모양 구하는 건줄 알고 식을 잘못 세웠다. 다시 읽어보니 가에 뭔가를 붙인다는 것 같아서 다시 세웠다. rhombus가 마름모인줄 알았다면 더 빨리 풀 수 있었을텐데! 아무튼 n-1번째 도형에 (n-1)*4를 더하면 n이 되므로 1부터 n까지 반복문을 써서 더해줬다. 식을 구하면 O(1)에 구할 수 있는데 입력이 100밖에 안 되고 식 구하느라 시간 보내는 것 보다 빨리 풀고 끝내는게 나을 것 같아서 반복문으로 해결했다.

 

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <string>
 
using namespace std;
using i64 = long long;
 
int main() {
    int n;
    scanf("%d", &n);
 
    int count = 1;
    for(int i = 1; i < n; i++){
        count += i*4;
    }
    printf("%d", count);
    
    return 0;
}

 

대회 끝난 김에 다시 풀어보면 식을 저렇게 세울 수 있겠다. 왕년의 고등학교 기억을 꺼내보면 홀수의 합은 제곱으로 나타낼 수 있다. 1+3+5 = 9, 1+3+5+7 = 16 진짜임. 아무튼 이걸 사용하면 O(1)로 풀 수 있다. 저거 풀어서 적는 것도 가능한데 굳이 싶어서 저 그대로 짰음. 

 

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <string>
 
using namespace std;
using i64 = long long;
 
int main() {
    int n;
    scanf("%d", &n);
 
    printf("%d", n*n + (n-1)*(n-1));
    
    return 0;
}

예아