[프로그래머스] 직사각형 별찍기

Rae-eun Yang·2022년 7월 21일
0

프로그래머스

목록 보기
16/83

문제 설명


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


제한 조건


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

예시

입력

5 3

출력

*****
*****
*****

풀이 코드 (Java)

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();
        }
    }
}


풀이 코드 2 (Java)

import java.util.*;
import java.io.*;

class Solution {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int a = Integer.parseInt(st.nextToken());
        int b = Integer.parseInt(st.nextToken());
        
        for(int i = 0; i < b; i++) {
            for(int j = 0; j < a; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

  • BufferedReader, InputStreamReader, StringTokenizer 를 사용해서 입력 받기

풀이 코드 (JavaScript)

process.stdin.setEncoding('utf8');
process.stdin.on('data', data => {
    const n = data.split(" ");
    const a = Number(n[0]), b = Number(n[1]);
    let line = '';
    
    for(let i = 0; i < b; i++) {
        for(let j = 0; j < a; j++) {
            line += '*';
        }
        line += '\n';
    }
    
    console.log(line);
});


profile
개발자 지망생의 벨로그

0개의 댓글