알고리즘

[백준] - 2441, 별찍기4

hatch 2023. 2. 14. 10:16
728x90
반응형

* 공부 목표

입출력 - 2557, 1000, 2558, 1095010951, 10952, 1095311021, 11022, 11718, 1171911720, 117212741, 2742, 27391924, 8393, 108182438, 2439, 2440, 2441, 2442, 2445, 2522, 2446, 10991, 10992

 

  • 2441번

문) 첫째 줄에는 별 N개, 둘째 줄에는 별 N-1개, ..., N번째 줄에는 별 1개를 찍는 문제

하지만, 오른쪽을 기준으로 정렬한 별(예제 참고)을 출력하시오.

 

** 이전부터 했던 별찍기 활용인데, for문에 대한 이해가 낮았는지 기억이 안나더라...

게시글 참고해서 답안 제출함.

 

1) Scanner 활용

import java.util.*;

public class Main{
    public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int N = sc.nextInt();
		sc.close();
		
		for(int j=0; j<N; j++) {
			for(int a=N; a<j+N; a++) {
				System.out.print(" ");
			}
			for(int b=j+1; b<N+1; b++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

 

2) BufferedReader + BufferedWriter 활용 // 시간이 말도 안되게 절약된다!!!

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
		
		int N = Integer.parseInt(br.readLine());
		br.close();
		
		for(int j=0; j<N; j++) {
			for(int a=N; a<j+N; a++) {
				bw.write(" ");
			}
			for(int b=j+1; b<N+1; b++) {
				bw.write("*");
			}
			bw.write("\n");
		}
		bw.flush();
		bw.close();
	}
}

 

[참고 게시글] https://yy-eun.tistory.com/83

 

[백준] - 2438, 2439(서칭) 별찍기

* 공부 목표 입출력 - 2557, 1000, 2558, 10950, 10951, 10952, 10953, 11021, 11022, 11718, 11719, 11720, 11721, 2741, 2742, 2739, 1924, 8393, 10818, 2438, 2439, 2440, 2441, 2442, 2445, 2522, 2446, 10991, 10992 2438번 문) 첫째 줄에는 별 1개,

yy-eun.tistory.com