![[백준 / BOJ] 2438번 별 찍기 - 1 (C++, Python)](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FcPsLCH%2FbtrJcE8jBBM%2FCi1LkLJvv1Fwke1k5K1511%2Fimg.png)
[백준 / BOJ] 2438번 별 찍기 - 1 (C++, Python)◎ 자료구조와 알고리즘/백준(BOJ) 문제풀이2021. 8. 23. 23:37
Table of Contents
반응형
링크 : https://www.acmicpc.net/problem/2438
2438번: 별 찍기 - 1
첫째 줄에는 별 1개, 둘째 줄에는 별 2개, N번째 줄에는 별 N개를 찍는 문제
www.acmicpc.net
문제

문제 풀이
활용도가 무궁무진한 별 찍기 문제다.
C++ 상세 풀이
C++ 풀이 펼쳐보기
for (int i=1; i<=val; i++)
{
for (int j=0; j<i; j++)
{
std::cout << "*";
}
std::cout << "\n";
}
입력받은 값에 따라 별이 늘어나므로, 이중 for문을 사용하자. 우선 값을 받은 만큼 for문을 돌려주고, 그 안에 for문을 한번 더 넣어 밖의 i만큼 별을 찍어주도록 하자.
Python 상세 풀이
Python 풀이 펼쳐보기
for i in range(1, val + 1):
print("*" * i)
파이썬의 print 함수는 굉장히 편리한 기능을 많이 제공하기 때문에 간단하게 표현된다.
C++ 코드 전문
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
int main() | |
{ | |
int val; | |
std::cin >> val; | |
for (int i=1; i<=val; i++) | |
{ | |
for (int j=0; j<i; j++) | |
{ | |
std::cout << "*"; | |
} | |
std::cout << "\n"; | |
} | |
} |
Python 코드 전문
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val = int(input()) | |
for i in range(1, val + 1): | |
print("*" * i) |
소감
반응형
'◎ 자료구조와 알고리즘 > 백준(BOJ) 문제풀이' 카테고리의 다른 글
[백준 / BOJ] 10871번 X보다 작은 수 (C++, Python) (0) | 2021.08.23 |
---|---|
[백준 / BOJ] 2439번 별 찍기 - 2 (C++, Python) (0) | 2021.08.23 |
[백준 / BOJ] 11022번 A+B - 8 (C++, Python) (0) | 2021.08.23 |
[백준 / BOJ] 11021번 A+B - 7 (C++, Python) (0) | 2021.08.23 |
[백준 / BOJ] 2742번 기찍 N (C++, Python) (0) | 2021.08.23 |
@Reo :: 코드 아카이브
자기계발 블로그