알고리즘

[hackerRank] datatypes

91cm 2020. 9. 29. 18:02


문제

https://www.hackerrank.com/challenges/java-datatypes



접근방법

- 각 자료형의 최소값, 최대값을 자바에서 지원해 주기 때문에, 

자료형의 크기 순서만 안다면 조건문으로 쉽게 처리 가능.



코드

package com.cm;

import java.util.Scanner;

/**
* https://www.hackerrank.com/challenges/java-datatypes
*/
public class JavaType {

public JavaType() {
solution();
}

public void solution(){
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 >= Byte.MIN_VALUE && x <=Byte.MAX_VALUE){
System.out.println("* byte");
System.out.println("* short");
System.out.println("* int");
System.out.println("* long");
}
else if(x >= Short.MIN_VALUE && x <= Short.MAX_VALUE){
System.out.println("* short");
System.out.println("* int");
System.out.println("* long");
}
else if(x >= Integer.MIN_VALUE && x <= Integer.MAX_VALUE){
System.out.println("* int");
System.out.println("* long");
}
else if (x >= Long.MIN_VALUE && x <= Long.MAX_VALUE){
System.out.println("* long");
}
} catch (Exception e) {
System.out.println(sc.next() + " can't be fitted anywhere.");
}
}
}
}