【问题标题】:Finding the appropriate Java Datatype查找适当的 Java 数据类型
【发布时间】:2015-06-08 07:15:58
【问题描述】:
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    while (input.hasNextLine()) {
        BigInteger number = new BigInteger(input.nextLine());

        int bitLength = number.bitlength();
        if (bitLength <= Bytes.SIZE)
            System.out.println("\u8211 byte");
        if (bitLength <= Short.SIZE)
            System.out.println("\u8211 short");
        if (bitLength <= Int.SIZE)
            System.out.println("\u8211 int");
        if (bitLength <= Long.SIZE)
            System.out.println("\u8211 long");

        if (bitLength > Long.SIZE)
            System.out.println(number + " can't be fitted anywhere.");
    }
} 

任务:寻找合适的数据类型 样本输入:5

-150
 150000
 1500000000
 213333333333333333333333333333333333
-100000000000000

样本输出:

-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

错误 1:

error: cannot find symbol
    int bitLength = number.bitlength();
                      ^

错误 2:

symbol:   method bitlength()
location: variable number of type BigInteger

错误 3:

error: cannot find symbol
    if (bitLength <= Int.SIZE)
                 ^
    symbol:   variable Int
    location: class Solution

【问题讨论】:

  • int number = input.nextInt(); 不能返回大于 int 的值
  • 这是bitLength 而不是bitlength,我已经在我的回答中解决了这个问题。
  • 不要在此处发布整个解决方案,因为这会使其他人很容易复制您的解决方案。你应该在这里留下足够的东西来解决一个问题,即你原来的问题是如何计算所需的位数。
  • 整数对象类型是java.lang.Integer,而不是Int
  • @SandeepGV 在hackerrank之类的网站上解决问题的整个想法不就是让自己真正解决问题并从中学习吗?

标签: java int byte long-integer short


【解决方案1】:

逐行读取数字。使用BigInteger 计算位并将其除以8 以简化switch 大小写。看看下面的代码:

    Scanner input = new Scanner(new File("so/input.txt"));
    while (input.hasNextLine()) {
        BigInteger number = new BigInteger(input.nextLine().trim());
        int bitLength = number.bitLength();
        int len = bitLength / 8;
        StringBuilder output = new StringBuilder(number.toString() + " can be fitted in:\n");
        switch (len) {
            case 0:
                output.append(" byte");
            case 1:
                output.append(" short");
            case 2:
            case 3:
                output.append(" int");
            case 4:
            case 5:
            case 6:
            case 7:
                output.append(" long");
                System.out.println(output);
                break;
            default:
                System.out.println(number.toString() + "  can't be fitted anywhere.");
        }
    }

【讨论】:

    【解决方案2】:

    错误:非法字符:\8211 在每个 If 语句之前

    要将这个字符放入您的代码\u8211

    if语句以及如何输入任何数据类型都不能容纳的数字?

    您需要使用可以容纳和编号的数据类型。

    试试这个吧。

    while (input.hasNextLine()) {
        BigInteger number = new BigInteger(input.nextLine());
    
        int bitLength = number.bitLength() + 1;
        if (bitLength <= Bytes.SIZE)
             System.out.println(" \u8211 byte");
    
        if (bitLength <= Short.SIZE)
             System.out.println(" \u8211 short");
    
        // more checks.
    
        if (bitLength > Long.SIZE)
            // too big.
    

    解决了这个问题,还有很多工作要做,但使用 BigInteger.bitLength() 是更优雅的解决方案。

    找不到符号 if (bitLength

    Java 中没有Int 类型,它是Integer

    【讨论】:

      【解决方案3】:

      你可以简单地把条件和数据类型的范围放在一起,检查输入的数字是否属于哪个数据类型。

          class FindDataType {
      public static void main(String[] argh) {
          Scanner sc = new Scanner(System.in);
          //no. of input values
          int t = sc.nextInt();
          for (int i = 0; i < t; i++) {
              try {
                  //Take input as long data type
                  long x = sc.nextLong();
                  System.out.println(x + " can be fitted in:");
                  //Putting conditions to check the data type
                  if (x >= -128 && x <= 127) {
                      System.out.println("* byte");
                      System.out.println("* short");
                      System.out.println("* int");
                      System.out.println("* long");
                  } else if (x >= -32768 && x <= 32767) {
                      System.out.println("* short");
                      System.out.println("* int");
                      System.out.println("* long");
                  } else if (x >= -2147483648 && x <= 2147483647) {
                      System.out.println("* int");
                      System.out.println("* long");
                  } else if (x >= -9223372036854775808l
                          && x <= 9223372036854775807l) {
                      System.out.println("* long");
                  }
              } catch (Exception e) {
                  //Printing exception if no data type matches.
                  System.out.println(sc.next() + " can't be fitted anywhere.");
              }
      
          }
          sc.close();
      }}
      

      【讨论】:

        【解决方案4】:
          int t = sc.nextInt();
          for (int i = 0; i < t; i++) {
              BigInteger x = sc.nextBigInteger();
              int bitLength = x.bitLength() + 1;
              StringBuilder output= new StringBuilder(x.toString() + " can be fitted in:\n");
              if (bitLength <= Byte.SIZE)
                  output.append("* byte\n");
              if (bitLength <= Short.SIZE)
                  output.append("* short\n");
              if (bitLength <= Integer.SIZE)
                  output.append("* int\n");
              if (bitLength <= Long.SIZE)
                  output.append("* long\n");
        
              if (output.subSequence(output.indexOf(":"),output.length()-1).length() >    1) {
                  System.out.print(output);
        
              } else {
                  System.out.println(x + " can't be fitted anywhere.");
              }
          }
        

        【讨论】:

          【解决方案5】:
          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 >= -(Math.pow(2, 15)) && x <= (Math.pow(2, 15) - 1)) System.out.println("* short");
                      if (x >= -(Math.pow(2, 31)) && x <= (Math.pow(2, 31) - 1)) System.out.println("* int");
                      if (x >= -(Math.pow(2, 63)) && x <= (Math.pow(2, 63) - 1)) System.out.println("* long");
          
                  } catch (Exception e) {
                      System.out.println(sc.next() + " can't be fitted anywhere.");
                  }
              }
            }
          }
          

          【讨论】:

            猜你喜欢
            • 2011-07-15
            • 2015-06-08
            • 2010-10-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多