[HackerRank]-Java Datatypes
두번 째 해커랭크 문제 기초이다.
Sample Input
5
-150
150000
1500000000
213333333333333333333333333333333333
-100000000000000
Sample Output
-150 can be fitted in:
- short
- int
- long
150000 can be fitted in:
- int
- long
1500000000 can be fitted in:
- int
- long
213333333333333333333333333333333333 can’t be fitted anywhere.
-100000000000000 can be fitted in:
- long
예시 설명
5개의 수를 입력 후
범위안에 있는 숫자 데이터타입을 모두 출력한다.
- byte : -128 <= n <= 127
- short : -32,768 <= n <= 32,767
- int : -2^31 <= n <= 2^31-1
- long : -2^63 <= n <= 2^63-1
여기서 포인트는 숫자의 데이터 타입 범위 이다.
import java.util.*;
import java.io.*;
class Solution{
public static void main(String []argh)
{
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++)
{
try
{
long x=sc.nextLong();
System.out.println(x+" can be fitted in:");
if(x>=-128 && x<=127)System.out.println("* byte");//작성부분
if(x>=-32768 && x<=32767)System.out.println("* short");
if(x>=-Math.pow(2,31) && x<=Math.pow(2,31)-(long)1)System.out.println("* int");
if(x>=-Math.pow(2,63) && x<=Math.pow(2,63)-(long)1)System.out.println("* long");//-----여기까지
//Complete the code
}
catch(Exception e)
{
System.out.println(sc.next()+" can't be fitted anywhere.");
}
}
}
}
설명
소스코드 형식은 이미 장석되어 있고 나머지 부분을 채우면 된다. 나머지 부분은 if 문 4개 이다.
요약
숫자 데이터타입 범위만 알면 쉬운 문제이다. Math.pot()를 알아두면 좋다.