题目描述:
Integer in C++
Problem Description
KIDx: I like Java much more than C++, because I can use BigInteger in Java. :)
However, KIDx has to use C++ language to do a project...
Little KIDx knows 3 integer types in C++:
1) short occupies 2 bytes and allows you to store numbers from -32768 to 32767
2) int occupies 4 bytes and allows you to store numbers from -2147483648 to 2147483647
3) long long occupies 8 bytes and allows you to store numbers from -9223372036854775808 to 9223372036854775807
For all the types given above the boundary values are included in the value range. From this list, KIDx wants you to choose the smallest type that can store a positive integer n.
Input
Each case contains a positive integer n. It consists of at least one digit and at most 30 digits. In addition, it doesn't contain any leading zeros.
Output
For each line, print the first type from the list "short, int, long long", that can store the natural number n. If no one can store the number, just print "It is too big!".
Sample Input
102 50000 123456789101112131415161718192021222324
Sample Output
short int It is too big!
简单字符串处理,留作纪念!!!
竟然忘记了strcmp这个函数的强大功能,它的比较是按ASCII码表的。
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <cstdlib> 5 using namespace std; 6 7 const int maxn = 30 + 10; 8 char s[maxn], t[maxn]; 9 10 int main() 11 { 12 int i, len; 13 while (gets(s)) 14 { 15 len = strlen(s); 16 if (s[0] == '-') 17 { 18 int l = 0; 19 for (i = 1; i < len; i++) 20 t[l++] = s[i]; 21 if ((strcmp(t, "32768") <= 0 && l == strlen("32768")) || l < strlen("32768")) 22 puts("short"); 23 else if ((strcmp(t, "2147483648") <= 0 && l == strlen("2147483648")) || l < strlen("2147483648")) 24 puts("int"); 25 else if ((strcmp(t, "9223372036854775808") <= 0 && l == strlen("9223372036854775808")) || l < strlen("9223372036854775808") ) 26 puts("long long"); 27 else 28 puts("It is too big!"); 29 } 30 else 31 { 32 if ((strcmp(s, "32767") <= 0 && len == strlen("32767")) || len < strlen("32767")) 33 puts("short"); 34 else if ((strcmp(s, "2147483647") <= 0 && len == strlen("2147483647")) || len < strlen("2147483647")) 35 puts("int"); 36 else if ((strcmp(s, "9223372036854775807") <= 0 && len == strlen("9223372036854775807")) || len < strlen("9223372036854775807")) 37 puts("long long"); 38 else 39 puts("It is too big!"); 40 } 41 } 42 return 0; 43 }