문제
정수 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
'프로그래머스코딩테스트연습풀이 > C++' 카테고리의 다른 글
[프로그래머스/C++] 주사위의 개수 (0) | 2025.02.20 |
---|---|
[프로그래머스/C++] 간단한 식 계산하기 (0) | 2025.02.20 |
[프로그래머스/C++] 암호 해독 (0) | 2025.02.20 |
[프로그래머스/C++] l로 만들기 (0) | 2025.02.20 |
[프로그래머스/C++] 모음 제거 (0) | 2025.02.20 |