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

allnight5·2023년 3월 22일
0

프로그래머스

목록 보기
48/73

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

자바 첫번째 풀이

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

개선

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

        StringBuilder sb = new StringBuilder();
        for(int i=0; i<a; i++){
            sb.append("*");
        }
        for(int i=0; i<b; i++){
            System.out.println(sb.toString());
        }
    }
}

파이썬

a, b = map(int, input().strip().split(' '))
answer = ('*'*a +'\n')*b
print(answer)

파이썬은 문자열 곱하기(곱한 숫자만큼 출력)가 가능하다

profile
공부기록하기

0개의 댓글