728x90
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
문제 풀이
입력값만큼 별을 찍어주면 된다. 입력값이 5일 경우, 별은 1개부터 5개까지 찍히며, 반복문을 이용해주었다.
정답 코드
C++
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
#define fast ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0)
#define ll long long
int main() {
fast;
int n;
while (1) {
cin >> n;
if (n == 0) break;
for (int i = 1; i < n + 1; i++) {
for (int j = 0; j < i; j++) {
cout << "*";
}
cout << "\n";
}
}
}
python
while True :
n = int(input())
if n == 0 : break
for i in range(1, n + 1) :
for _ in range(i) :
print("*", end = "")
print()
728x90
'Algorithm > PS' 카테고리의 다른 글
[BOJ/백준] 26531 - Simple Sum [python] (0) | 2023.03.06 |
---|---|
[BOJ/백준] 13116 - 30번 [C++/python] (0) | 2023.03.04 |
[BOJ/백준] 6888 - Terms of Office [C++/python] (0) | 2023.03.02 |
[BOJ/백준] 6887 - Squares [C++/python] (0) | 2023.03.02 |
[BOJ/백준] 6841 - I Speak TXTMSG [C++/python] (0) | 2023.03.02 |