Problem Description
Problem Analysis
Determine the data type of a number
Except for BigInteger, all others can be determined directly
So the main task is to determine whether it is BigInteger
This can be based on the number of digits
Then we only need to handle cases where the digit count equals that of the boundary value
The rest is some small tricks
| |
Problem link:【CodeForces】[66A]Petya and Java
Statement:
Given a number, output its data type
- 128~127 is byte
- 32768~32767 is short
- 2147483648~2147483647 is int
- 9223372036854775808~9223372036854775807 is long
All others are BigInteger
Solution process:
Because the number of digits is large
it can be read as a string
Except for BigInteger, all others are within the range of __int64
So if we can determine that the number is in - 9223372036854775808~9223372036854775807
then it can be converted to a number, stored in __int64, and further range-checked
So the main task is to determine whether it is BigInteger
This can be preliminarily determined by the number of digits
It can be seen that
Positive numbers with a string length greater than 19 (s[0]!=’-’) and negative numbers with a length greater than 20 (s[0]==’-’)
must be BigInteger
Positive numbers with a string length less than 19 (s[0]!=’-’) and negative numbers with a length less than 20 (s[0]==’-’)
must not be BigInteger
So we only need to handle the equality case
Then we can create a string “9223372036854775807”
and compare it with the string to make the determination