문제
Observe that its base and height are both equal to n, and the image is drawn using # symbols and spaces. The last line is not preceded by any spaces.
Write a program that prints a staircase of size n.
Function Description
Complete the staircase function in the editor below. It should print a staircase as described above.
Input Format
A single integer, n, denoting the size of the staircase.
Output Format
Print a staircase of size n using # symbols and spaces.
CODE
void staircase(int n) {
int i; // 줄 번호
for(i=0;i<n;i++){
for(int j=n-i-1;j>0;j--) // space 입력
printf(" ");
for(int j=0;j<=i;j++) // # 입력
printf("#");
printf("\n"); // 한 줄의 내용이 끝나면 개행한다
}
}
감상
for문을 int j로 초기화하여 변수를 세 개 쓰는 일 없이 두 개로 풀 수 있게 하였다. 어떻게 하면 더 간단하게 풀 수 있을까?
+) java로 푸는 방법 소개 클릭
'문제풀이 > C 문제풀이' 카테고리의 다른 글
[HackerRank]Birthday Cake Candles (0) | 2019.06.26 |
---|---|
[HackerRank]Mini-Max Sum (0) | 2019.06.25 |
[HackerRank]Plus Minus (0) | 2019.05.24 |
[HackerRank]Diagonal Difference (0) | 2019.05.21 |
[Hackerrank]A Very Big Sum (0) | 2019.05.20 |