-
[HackerRank] Staircase c++Coding Test/HackerRank 2022. 7. 16. 22:31728x90
- problem
This is a staircase of size n =4 :
# ## ### ####
Its base and height are both equal to n. It 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.
- sample input
6
- sample output
# ## ### #### ##### ######
- explanation
The staircase is right-aligned, composed of # symbols and spaces, and has a height and width of n = 6.
- solution
void staircase(int n) { string res(""); for(int i = 0; i < n; i++){ string s(""); for(int j = 0; j < n-i -1; j++ ){ s += " "; } for(int j = n-i -1; j < n; j++){ s += "#"; } res += s + "\n"; } cout << res; }
728x90'Coding Test > HackerRank' 카테고리의 다른 글
[HackerRank] Birthday Cake Candles c++ (0) 2022.07.18 [HackerRank] Mini-Max Sum c++ (0) 2022.07.16 [HackerRank] Plus Minus c++ (0) 2022.07.16 [HackerRank] Diagonal Difference c++ (0) 2022.07.16 [HackerRank] A Very Bing Sum c++ (0) 2022.07.16