Computer/Coding Test
Exercism - Pascal's Triangle 파스칼 삼각형
안녕도라
2025. 2. 13. 12:59
문제요약
행의 갯수를 입력하면 파스칼 삼각형 규칙에 맞는 행 모음을 출력하라.

https://exercism.org/tracks/csharp/exercises/pascals-triangle
Pascal's Triangle in C# on Exercism
Can you solve Pascal's Triangle in C#? Improve your C# skills with support from our world-class team of mentors.
exercism.org
난이도
Medium
풀이 포인트
- 파스칼 삼각형 규칙 이해
- 2차원 배열 인덱싱 헷갈리지 않기
REVIEW
오랜만에 만난 파스칼 삼각형.
0부터 인덱싱 되는 배열과 달리 파스칼 삼각형의 행은 1부터 인덱싱해야해서
그것만 주의하면 되는 간단한 문제.
Excercism의 Medium 난이도가 어떤 건 백준 골드1은 될거 같고, 어떤 건 실버 1도 안될 것 같은 문제들이 섞여있다.
CODE
using System;
using System.Collections.Generic;
public static class PascalsTriangle
{
public static IEnumerable<IEnumerable<int>> Calculate(int rows)
{
List<List<int>> answer = new List<List<int>>();
int currentRow = 0;
while (currentRow < rows)
{
List<int> row = new List<int>();
for (int i = 0; i <= currentRow; i++)
{
if (i == 0 || i == currentRow)
{
row.Add(1);
}
else
{
row.Add(answer[currentRow - 1][i - 1] + answer[currentRow - 1][i]);
}
}
answer.Add(row);
currentRow++;
}
return answer;
}
}