알고리즘/백준
백준 2444 [자바 java]
발빠진 쥐
2024. 7. 2. 17:29
문제
예제를 보고 규칙을 유추한 뒤에 별을 찍어 보세요.
입력
첫째 줄에 N(1 ≤ N ≤ 100)이 주어진다.
출력
첫째 줄부터 2×N-1번째 줄까지 차례대로 별을 출력한다.
예제 입력 1
5
예제 출력 1
*
***
*****
*******
*********
*******
*****
***
*
별찍기 너무 어렵듀
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
for (int i=1; i<=N; i++) {
for(int j=0; j<N-i; j++) {
System.out.print(" ");
}
for(int j=0; j<2*i-1; j++) {
System.out.print("*");
}
System.out.println();
}
for (int i=N-1; i>=0; i--) {
for(int j=0; j<N-i; j++) {
System.out.print(" ");
}
for(int j=0; j<2*i-1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
이게 맞는코드
이게 잘못 된 코드 (수정 전)
변수 i를 for문안에 있는 for문에 사용해서
반복하는 횟수 + 안쪽 for문에서의 계산 값
두개 다 생각 해줘야 되는게 어려웟다...