직사각형 별찍기 - 1단계

이동명·2023년 6월 2일
0
post-thumbnail

문제 설명

이 문제에는 표준 입력으로 두 개의 정수 n과 m이 주어집니다.
별(*) 문자를 이용해 가로의 길이가 n, 세로의 길이가 m인 직사각형 형태를 출력해보세요.

제한 조건

n과 m은 각각 1000 이하인 자연수입니다.
예시
입력

5 3

import java.util.Scanner;

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

    }
}:
*****
*****
*****

지루한 별찍기. 나 역시도 2000명여과 같은 일반적인 코드를 짯다.
하지만 이런 지루한 별찍기도 색다르게 푼 풀이가 있었다.

다른사람의 풀이

import java.util.Scanner;
import java.util.stream.IntStream;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();

        StringBuilder sb = new StringBuilder();
        IntStream.range(0, a).forEach(s -> sb.append("*"));
        IntStream.range(0, b).forEach(s -> System.out.println(sb.toString()));
    }
}

오호....이렇게도....

profile
Web Developer

0개의 댓글