[C] LV.2 삼각 달팽이

2021. 11. 4. 15:38·Algorism(PS)/프로그래머스
728x90
 

코딩테스트 연습 - 삼각 달팽이

5 [1,2,12,3,13,11,4,14,15,10,5,6,7,8,9] 6 [1,2,15,3,16,14,4,17,21,13,5,18,19,20,12,6,7,8,9,10,11]

programmers.co.kr

내 코드

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

int* solution(int n) {
    // return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞게 변경해주세요.
    int* answer = (int*)calloc(n * (n+1) / 2, sizeof(int));
    
    int index = 0; // 인덱스 값
    int count = 0; // 핸들링할 값, 배열간 간격조절
    int handle = 0; // 진행방향
    int value = 1 // 삽입할 값

    for(int i = 0; i < n; i++)
    {
        for(int j = i; j < n; j++) 
        {
            if(handle == 0)
            {
                index += count; 
                count++;
                answer[index] = value;
                value++;
            }
            else if(handle == 1)
            {
                ++index;
                answer[index] = value;
                value++;
            }
            else if(handle == 2)
            {
                index -= count;
                count--;
                answer[index] = value;
                value++;
            }
        }
        handle++;
        if(handle == 3) handle = 0;
    }
    
    return answer;
}

한 수 배울 코드

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

int* solution(int n) {
    // return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞게 변경해주세요.
    int* answer = (int*)calloc(n * (n+1) / 2, sizeof(int));
    int state = 0, index = 0, count = 1;

    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n - i; j++) {
            if(state%3 == 0) {
                index = index - state/3 + i + j;
            } else if (state%3 == 1){
                index++;
            } else {
                index = index - n + state/3 + j;
            }
            answer[index] = count++;
        }
        state++;
    }
    return answer;
}
  • 깔끔하다. 내 코드에서 불필요한 부분을 잘 축약한 코드같은 느낌.
  • 상태를 3가지 케이스로 나누는 것 까지는 같음.
  • 인덱스 값을 정하는 방식 말고는 대개 비슷한듯.

어려웠다. 거의 2시간 가까이 푼듯.. C도 안쓰다 쓰려니 힘들다. 기초부터 다시

내 C언어로는 Level.2부터 천천히 문제를 풀어봐야 할듯..

저작자표시

'Algorism(PS) > 프로그래머스' 카테고리의 다른 글

[C] LV.2 주식가격  (0) 2021.11.10
[C] LV.1 없는 숫자 더하기  (8) 2021.11.03
[C] LV.1 문자열을 정수로 바꾸기  (0) 2021.11.03
'Algorism(PS)/프로그래머스' 카테고리의 다른 글
  • [C] LV.2 주식가격
  • [C] LV.1 없는 숫자 더하기
  • [C] LV.1 문자열을 정수로 바꾸기
기짜낭
기짜낭
생각이 많지만, 지금 내가 해야할 것을 하자.
  • 기짜낭
    So tired
    기짜낭
    • 분류 전체보기 (203) N
      • Programming (14) N
        • HTML (0)
        • CSS (0)
        • Javascript (18)
        • SVG (1)
        • SASS (SCSS) (1)
        • Node.js (1)
        • MySQL (2)
        • Canvas (8)
        • React (14)
        • Typescript (6)
        • C (3)
        • Java (1)
      • 활동 (57)
        • 개인 프로젝트 (33)
        • 나눔 서포터즈 3기 (9)
        • 멋쟁이 사자처럼 (7)
        • 0&1 C++ 자료구조 스터디 (0)
        • 제 9회 창업 아이디어톤 (3)
        • Poom (ZeroWasteShop) (3)
        • 해커톤 (2)
      • Algorism(PS) (27)
        • 알고리즘 정리 (5)
        • 백준 (18)
        • 프로그래머스 (4)
      • 취미 (5)
        • 강연 (5)
      • 생각 (16)
      • 기타 (8)
      • Reference (17)
        • 용어 사이트 (7)
        • 유용한 사이트 (10)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

    • ※ 예전 블로그
  • 인기 글

  • 태그

    디자인
    Erica
    HTML
    우테코
    군대
    canvas
    개발자
    ES6
    한양대학교
    SVG
    타입스크립트
    HTML5
    나눔 서포터즈
    TypeScript
    백준
    대학
    프론트엔드
    tutorial
    3기
    알고리즘
    에리카
    Javascript
    프로그래밍
    1주에 1권씩
    프로젝트
    독후감
    1주 1권
    react
    개념
    CSS
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.0
기짜낭
[C] LV.2 삼각 달팽이
상단으로

티스토리툴바