22.4.17 [HackerRank]Java Datatypes


🌱 背景知識


Java言語の8つの基本タイプ



各資料型の大きさで数字の桁数を判断できます.
例えば、longは8バイトのデータ型であり、各バイトは8ビットを含む.
これは、合計64ビットの数字を意味します.
コンピュータはすべての数字をバイナリ形式で格納するので、MSBの
部分の1ビットで記号を判別する.(signed)
したがって,−2^63~2^63−1の範囲に表現できる.

▼▼解説と分析


入力数のデータサイズと対応するデータ型を決定します.
n can be fitted in:
* dataType
の形で表現すればいいです.複数のデータ型がある場合
1行ずつ出力し、サイズでソートします.

この4つのフォーマットがどこにも適用されない場合
n can't be fitted anywhere.
それを印刷すればいいです.
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");
                //Complete the code
                if(x>=-32768 && x<=32767)System.out.println("* short");
                // 예시가 있어서 힌트를 얻기 쉬웠던 것 같다. 실수형 범위를 지정해 주고 출력할 문자를 적어준다.
                if(x>=Math.pow(-2, 31) && x<=Math.pow(2,31)-1)System.out.println("* int");
                // int와 long 부터는 자리값이 너무 커져서 숫자를 전부 다 적어주는 것은 비효율적인 것 같다. 
                if(x>=Math.pow(-2, 63) && x<=Math.pow(2,63)-1)System.out.println("* long");
            }
            // 4바이트인 int는 총 32비트이므로 -2^31~2^31-1
        	// 8바이트인 long은 총 64비트이므로 -2^63~2^63-1로 범위를 나타낼 수 있다.
            //Loop II에서 배운 Math.pow를 통해 범위를 나타내 주었다.
            catch(Exception e)
            {
                System.out.println(sc.next()+" can't be fitted anywhere.");
            }

        }
    }
}

👉 リファレンス

  • https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
  • https://coderbycode.tistory.com/6
  • https://dokkiblog.tistory.com/19
  • https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=sssmate1&logNo=100175096019