๊ณต๋ถ€/๋ฐฑ์ค€ (Baekjoon)

[๋ฐฑ์ค€/ํŒŒ์ด์ฌ] 7595๋ฒˆ: Triangles

eunjuu 2023. 10. 12. 12:40
728x90

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

It is not hard to draw a triangle of stars of any given size. For example, a size 5 triangle would look like this (5 stars high and 5 stars wide):

*
**
***
****
*****

Your task is to draw triangles in a number of sizes. 

๐Ÿ“Ž https://www.acmicpc.net/problem/7595

 

7595๋ฒˆ: Triangles

Each line of input contains a single positive integer, n, 1 <= n <= 100. The last line of input contains 0. For each non-zero number, draw a triangle of that size. 

www.acmicpc.net

 

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ์ž…๋ ฅ

Each line of input contains a single positive integer, n, 1 <= n <= 100. The last line of input contains 0. For each non-zero number, draw a triangle of that size. 

 

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ์ถœ๋ ฅ

Output consists of triangles of the appropriate sizes. Each triangle is wider at the bottom. There are no gaps between the triangles. 

 

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ์˜ˆ์ œ


๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ์ฝ”๋“œ

while True:
    n = int(input())
    if n == 0:
        break
    for i in range(1, n+1):
        print('*'*i)

→ ์ด ์ฝ”๋“œ๋Š” ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์–‘์˜ ์ •์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ํ•ด๋‹น ํฌ๊ธฐ์˜ ์‚ผ๊ฐํ˜•์„ '*' ๋ฌธ์ž๋กœ ์ถœ๋ ฅํ•˜๋ฉฐ, 0์„ ์ž…๋ ฅํ•˜๋ฉด ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒํ•œ๋‹ค.

 

while True:
    n = int(input())
    if n == 0:
        break

while True ๋ฌดํ•œ๋ฃจํ”„๋ฅผ ์ด์šฉํ•ด 

  1. n = int(input()): ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ •์ˆ˜ ๊ฐ’์„ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค. int(input())๋ฅผ ํ†ตํ•ด ์ž…๋ ฅ ๊ฐ’์„ ์ •์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ณ€์ˆ˜ n์— ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
  2. if n == 0:: ์ž…๋ ฅ๊ฐ’ n์ด 0์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. 0์ด๋ฉด ๋ฌดํ•œ ๋ฃจํ”„์—์„œ ํƒˆ์ถœํ•˜๊ธฐ ์œ„ํ•ด break ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฃจํ”„๋ฅผ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.

 

while True:
    n = int(input())
    if n == 0:
        break
    for i in range(1, n+1):
        print('*'*i)
  1. for i in range(1, n+1):๋Š” 1๋ถ€ํ„ฐ n๊นŒ์ง€์˜ ๋ฒ”์œ„๋ฅผ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฒ”์œ„๋Š” ์‚ผ๊ฐํ˜•์˜ ํฌ๊ธฐ๋ฅผ ๊ฒฐ์ •ํ•ฉ๋‹ˆ๋‹ค.
  2. print('*'*i)๋Š” ํ˜„์žฌ i์˜ ๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” ๊ฐœ์ˆ˜๋งŒํผ * ๋ฌธ์ž๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰, i๊ฐ€ 1๋ถ€ํ„ฐ n๊นŒ์ง€ ์ฆ๊ฐ€ํ•˜๋ฉด์„œ ๊ฐ ๋ฃจํ”„์—์„œ * ๋ฌธ์ž๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ์ •๋‹ต ์ œ์ถœ

728x90