그럴듯한 개발 블로그
반응형

https://school.programmers.co.kr/learn/courses/30/lessons/68645#qna

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

#include <string>
#include <vector>
#include <cstdlib>
#include <iostream>
using namespace std;

/*
1
1 1
1 1 1
1 1 1 1
1 1 1 1 1
*/
vector<int> getAnswer(const vector<vector<int> > arr, int n) {
    vector<int> v;
    for (int i = 0; i < n; i++)
        for (int j = 0; j <= i; j++)
            v.push_back(arr[i][j]);
    return v;
}

vector<int> solution(int n) {
    vector<vector<int> > arr(n, vector<int>(n, 0));

    int i = 0, x = 0, y = 0;
    int command = 1;
    while (arr[x][y] == 0) { // 넣을 자리에 이미 들어가 있으면 종료
        arr[x][y] = ++i;
        if (command == 1 && (x >= n - 1 || arr[x + 1][y] != 0))
            command++;
        else if (command == 2 && (y >= n - 1 || arr[x][y + 1] != 0))
            command++;
        else if (command == 3 && arr[x - 1][y - 1] != 0) // 0 아니면 이미 채워진 거 => 내려가면 됨 cmd 1
            command = 1;

        if (command == 1)
            x++;
        else if (command == 2)
            y++;
        else if (command == 3) {
            x--; y--;
		}
    }
    return getAnswer(arr, n);
}

알고 계셨나요?

        else if (command == 3)
            x--; y--;

이렇게 한 줄로 줄여쓸 수 있지만 {}를 생략하고 쓸 순 없다는 사실!

이거 덕분에 두시간을 내다 버렸습니다....

알려주신 jahlee님께 그랜절 박습니다

반응형
profile

그럴듯한 개발 블로그

@donghyk2

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!