https://www.hackerrank.com/challenges/java-output-formatting
Take exactly 3 lines of input. Each line consists of a string and an integer. Suppose this is the sample input:
java 100
cpp 65
python 50
The strings will have at most 10 alphabetic characters and the integers will range between 0 to 999.
In each line of output there should be two columns. The string should be in the first column and the integer in the second column. This is the output for the input above:
================================
java 100
cpp 065
python 050
================================
The first column should be left justified using exactly 15 characters. The integer of the second column should have exactly 3 digits. If the original input has less than 3 digits, you should pad with zeros to the left.
To make the problem easier, some part of the solution is already given in the editor, just complete the remaining parts.
입력은 문자열은 최대 10자리, 정수형은 0부터 999로 입력이 된다.
출력시에는 첫번째 컬럼 즉 문자열은 15자리로 왼쪽 정렬이 되어야 하며, 두번째 컬럼 즉 정수형은 3자리여야 한다.
정수 값이 3자리 보다 작으면 왼쪽에 0으로 채운다.
보낸 답)
printf를 사용하여 문자열은 15자리인데 왼쪽정렬(-)을 하고(%-15s), 정수형은 3자리인데 앞에 부족한 부분은 0으로 채워놓는 형식(%03d)으로 해서 제출했다.
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 > HR-Java' 카테고리의 다른 글
#8 Java End-of-file (0) | 2015.11.23 |
---|---|
#6 Java Loops (0) | 2015.11.18 |
#4 Java Stdin and Stdout 2 (0) | 2015.11.17 |
#3 Java If-Else (0) | 2015.11.17 |
#2 Java Stdin and Stdout 1 (0) | 2015.11.17 |