문제 링크: https://www.acmicpc.net/problem/2438
백준 반복문 8단계 - 2438번 별 찍기 - 1을 풀어보았다.
풀이: 2중 반복문을 써서 풀었다.
파이썬의 경우, print에 end를 넣어 쓰면 문자를 옆으로 쓸 수 있다.
C++
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=0;j<i;j++)
{
cout<<"*";
}
cout<<"\n";
}
}
Python
n=int(input())
for i in range(n):
for j in range(i+1):
print('*',end='')
print('')
Java
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int i=1;i<=t;i++)
{
for(int j=0;j<i;j++)
{
System.out.print("*");
}
System.out.println("");
}
}
}
'코테용 문제풀이 > 백준' 카테고리의 다른 글
Hello World 풀이 (0) | 2022.12.30 |
---|---|
X보다 작은 수 풀이 (0) | 2022.12.30 |
백준 1009번 문제 분산처리 c#으로 풀기 (0) | 2019.06.08 |
백준 1004번 문제 어린 왕자 c#으로 풀기 (0) | 2019.06.08 |
백준 1003번 문제 피보나치 함수 c#으로 풀기 (0) | 2019.06.07 |