반응형
문제 (링크)
문제 설명
Case 1
input이 아래와 같을 때
12
4.0
is the best place to learn and practice coding!
|
cs |
nextInt()로 12 저장
nextDouble()로 4.0 저장
nextLine()로 문장 전체가 저장될 것 같지만 안된다.
처음에 바로 nextLine()로 저장했다면 됐을 테지만, 이미 앞에 무언갈 저장한 이후이기 때문에
next()를 사용해 한 단어만 저장한 후에 nextLine()을 사용해 나머지 문장을 저장해서 더하면 된다.
위의 인풋을 예로 들면 next()에는 "is"가, nextLine()에는 "the best place to learn and practice coding!"가 저장되는 것이다.
Case 2
scan.skip 사용하기
scan.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");을 사용하게 되면 입력값이 포함되지 않은 라인을 건너뛸 수 있게 된다.
scan.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");의 의미에 대해 자세히 알고 싶다면 밑의 링크를 참조하면 된다.
https://stackoverflow.com/questions/52111077/explain-this-line-written-in-java
답 (Java)
Case 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
int i = 4;
double d = 4.0;
String s = "HackerRank ";
Scanner scan = new Scanner(System.in);
int iScan = scan.nextInt();
double dScan = scan.nextDouble();
String sScan = scan.next() + scan.nextLine();
System.out.println(i + iScan);
System.out.println(d + dScan);
System.out.print(s + sScan);
scan.close();
|
cs |
Case 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
int i = 4;
double d = 4.0;
String s = "HackerRank ";
Scanner scan = new Scanner(System.in);
int iScan = scan.nextInt();
double dScan = scan.nextDouble();
scan.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
String sScan = scan.nextLine();
System.out.println(i + iScan);
System.out.println(d + dScan);
System.out.print(s + sScan);
scan.close();
|
cs |
그리고 color sprinter가 다시 된다! 저번에는 서버가 잠깐 터졌던것같다 ^^;;;
반응형