【发布时间】: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