별 찍기 직각삼각형 4
문제
테두리의 행과 열 번호는 출력하지 않아도 된다.
0 1 2 3 4
0 * * * * *
1 * * * *
2 * * *
3 * *
4 *
해답
행 번호와 열 번호를 비교해서 출력
#include <stdio.h>
int main(void)
{
for (int row = 0; row < 5; row++)
{
for (int col = 0; col < 5; col++)
{
if (col >= row)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
}
공백과 별을 계산해서 출력
#include <stdio.h>
int main(void)
{
for (int row = 0; row < 5; row++)
{
for (int col = 0; col < row; col++)
{
printf(" ");
}
for (int col = 0; col < 5 - row; col++)
{
printf("*");
}
printf("\n");
}
}
한 행에서 필요한 공백과 별의 개수는 다음과 같다.
- 0행: 공백 0개, 별 5개
- 1행: 공백 1개, 별 4개
- 2행: 공백 2개, 별 3개
- 3행: 공백 3개, 별 2개
- 4행: 공백 4개, 별 1개