728x90
반응형
* 공부 목표
입출력 - 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
- 10991번
문) 예제를 보고 규칙을 유추한 뒤에 별을 찍어 보세요.
출) 첫째 줄부터 N번째 줄까지 차례대로 별을 출력한다.
1. Scanner
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
sc.close();
String[] star = new String[num];
for(int i=1; i<num+1; i++) {
star[i-1] = " ".repeat(num-i) + "* ".repeat(i);
}
for(int i=0; i<num; i++) {
System.out.println(star[i]);
}
}
}
2. Br + Bw // Scanner에 비해 메모리도 적게, 시간도 절반 정도 소요.
- 다른 분들 풀이 보니, 배열 안 쓰고 바로 출력하는 방향.
import java.util.*;
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 num = Integer.parseInt(br.readLine());
br.close();
String[] star = new String[num];
for(int i=1; i<num+1; i++) {
star[i-1] = " ".repeat(num-i) + "* ".repeat(i);
}
for(int i=0; i<num; i++) {
bw.write(star[i] + "\n");
}
bw.flush();
bw.close();
}
}
'알고리즘' 카테고리의 다른 글
[백준] DP - 1463번, 1로 만들기 (0) | 2023.06.16 |
---|---|
[백준] 입출력 - 10992번, 별찍기 17 (0) | 2023.03.23 |
[백준] 입출력 - 2446번, 별찍기 9 (0) | 2023.03.20 |
[백준] 입출력 - 2552번, 별찍기 12 (0) | 2023.02.23 |
[백준] 입출력 - 2445번, 별찍기5 (0) | 2023.02.17 |