Input Format
Every line of input will contain a String followed by an integer.
Each String will have a maximum of 10 alphabetic characters, and each integer will be in the inclusive range from 0 to 999.
해석>
input으로 들어가는 모든 행에는 문자열 + 정수 의 구조로 이루어져 있다.
각 문자열은 10개 이하의 알파벳이고, 정수는 0부터 999까지의 숫자이다.
Output Format
In each line of output there should be two columns:
The first column contains the String and is left justified using exactly 15 characters.
The second column contains the integer, expressed in exactly 3 digits; if the original input has less than three digits, you must pad your output's leading digits with zeroes.
해석>
출력은 2열로 나와야한다:
첫번째 열은 문자열, 15의 좌측정렬된 칸을 사용한다.
두번째 열은 정수로, 3자리로만 표현되어야한다.
만약 원래 입력된 숫자가 3자리 이하이면, 0으로 시작해서 3자리에 맞게 출력해야한다.
Sample Input
java 100 cpp 65 python 50
Sample Output
================================
java 100
cpp 065
python 050
================================
풀이>
이거는 문제 이해만하고 정규표현식만 알고 있으면 금방 푸는 문제
System.out.printf : format을 넣어서 프린트하라는 메소드
("") : 쌍따옴표 안에 어떻게 표현할 것인지 적어준다.
%뒤에 오는것이 표현 내용
,으로 구분해주고
,뒤에 첫번째 원소가 첫번째 %에 배치될 내용
,뒤에 두번째 원소가 두번째 %에 배치될 내용
String이 왼쪽에서 15자리까지 자리를 가져야 하니까
%-15s(% 정규표현식 시작을 알리는 기호, - 좌측부터, 15 15자리, s String)
Integer이 3자리이고, 모자라는것은 0으로 채워져야하니까
%03d(% 정규표현식 시작을 알리는 기호, 0으로채운다, 3자리의 d를. d Decimal=integer)
그리고 입력받은것 마다 다른 행에 출력해야 하니까
%n
그래서 답은
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("================================");
for(int i=0;i<3;i++){
String s1=sc.next();
int x=sc.nextInt();
System.out.printf("%-15s%03d%n", s1, x);
}
System.out.println("================================");
}
}
'매일매일 코딩연습! > Hackerrank' 카테고리의 다른 글
해커랭크2. Java Stdin and Stdout Ⅱ (0) | 2021.02.10 |
---|---|
해커랭크1. Java If-Else submission (0) | 2021.02.09 |