본문 바로가기
프로그래머스코딩테스트연습풀이/C++

[프로그래머스/C++] 특별한 이차원 배열 1

by 코코쵸마 2025. 2. 20.

문제

정수 n이 매개변수로 주어질 때, 다음과 같은 n × n 크기의 이차원 배열 arr를 return 하는 solution 함수를 작성해 주세요.

arr[i][j] (0 ≤ i, j < n)의 값은 i = j라면 1, 아니라면 0입니다.

 

솔루션

#include <string>
#include <vector>

using namespace std;

vector<vector<int>> solution(int n) {
    vector<vector<int>> answer;
    for(int i = 0 ; i < n; i++){
        vector<int> tmp;
        for(int j = 0; j < n; j++){
            if(i == j){
                tmp.push_back(1);
            }
            else{
                tmp.push_back(0);
            }
        }
        answer.push_back(tmp);
    }
    return answer;
}

 

https://school.programmers.co.kr/learn/courses/30/lessons/181833

 

프로그래머스

SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프

programmers.co.kr