본문 바로가기
코딩테스트/Java

[Level1] 직사각형 별찍기 답안 및 풀이

by SRin23 2021. 6. 13.

◇ 문제 설명

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

 

◇ 제한 조건

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

 

◇ 입출력 예시

 

◇ 초기 내용

※ [출처] 프로그래머스-코딩테스트 연습-문제명

※ 초기 내용을 참고하여 문제에 맞는 코드를 작성하세요.

import java.util.Scanner;

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

        System.out.println(a + b);
    }
}

 


◇ 답안

import java.util.Scanner;

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

 

◇ 실행결과

직사각형 별찍기 실행결과

 

◇ 출처

https://programmers.co.kr/learn/challenges

 

코딩테스트 연습

기초부터 차근차근, 직접 코드를 작성해 보세요.

programmers.co.kr

 

댓글