Little Petya has recently started attending a programming club. Naturally he is facing the problem of choosing a programming language. After long considerations he realized that Java is the best choice. The main argument in favor of choosing Java was that it has a very large integer data type, called BigInteger.
But having attended several classes of the club, Petya realized that not all tasks require using the BigInteger type. It turned out that in some tasks it is much easier to use small data types. That's why a question arises: "Which integer type to use if one wants to store a positive integer n?"
Petya knows only 5 integer types:
1) byte occupies 1 byte and allows you to store numbers from - 128 to 127
2) short occupies 2 bytes and allows you to store numbers from - 32768 to 32767
3) int occupies 4 bytes and allows you to store numbers from - 2147483648 to 2147483647
4) long occupies 8 bytes and allows you to store numbers from - 9223372036854775808 to 9223372036854775807
5) BigInteger can store any integer number, but at that it is not a primitive type, and operations with it are much slower.
For all the types given above the boundary values are included in the value range.
From this list, Petya wants to choose the smallest type that can store a positive integer n. Since BigInteger works much slower, Peter regards it last. Help him.
The first line contains a positive number n. It consists of no more than 100 digits and doesn't contain any leading zeros. The number ncan't be represented as an empty string.
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d).
Print the first type from the list "byte, short, int, long, BigInteger", that can store the natural number n, in accordance with the data given above.
127
byte
130
short
123456789101112131415161718192021222324
BigInteger
题意:给你一个数,根据题意所给数的范围,确定并输出出这个数的类型。
解题思路:这个题上说最多有100位的数,肯定是大数了,那么就用字符串来储存所输入的数字。然后处理这个数字:分三种情况:①位数小于19位的,肯定能用一个long long型变量储存,然后再用普通方法判断这个数的类型(肯定是long型,int型,short型,byte型了);②位数等于19位的,用strcmp函数将9223372036854775807与字符串进行比较(返回值大于等于0说明所输入的数字小于等于9223372036854775807,是long型,返回值小于0说明所输入的数字大于9223372036854775807,是BigInteger型);③位数大于19位的,肯定是BigInteger型了。。。
刚开始没看清题,以为输入的数字可正可负,就把负数情况也顺便判断了下,方法与正数判断一样,把临界值和strcmp返回值改改就行。。
#include <stdio.h> #include <string.h> int main() { char s[110]; while(scanf("%s",s)!=EOF) { int size=strlen(s); if(s[0]=='-') { if(size==20) { if(strcmp("-9223372036854775808",s)>=0) { printf("long\n"); } else { printf("BigInteger\n"); } } if(size<20) { long long tmp=0; long long ci=1; for(int i=size-1;i>0;i--) { tmp=tmp+ci*(s[i]-'0'); ci=ci*10; } if(tmp>2147483648) { printf("long\n"); } else { if(tmp>32768) { printf("int\n"); } else { if(tmp>128) { printf("short\n"); } else { printf("byte\n"); } } } } if(size>20) { printf("BigInteger\n"); } } else { if(size==19) { if(strcmp("9223372036854775807",s)>=0) { printf("long\n"); } else { printf("BigInteger\n"); } } if(size<19) { long long tmp=0; long long ci=1; for(int i=size-1;i>=0;i--) { tmp=tmp+ci*(s[i]-'0'); ci=ci*10; } if(tmp>2147483647) { printf("long\n"); } else { if(tmp>32767) { printf("int\n"); } else { if(tmp>127) { printf("short\n"); } else { printf("byte\n"); } } } } if(size>19) { printf("BigInteger\n"); } } } return 0; }
385


&spm=1001.2101.3001.11974&articleId=51427928&d=1&t=3&u=abb3a2bc9c3f4663984b881f48d44880)

被折叠的 条评论
为什么被折叠?



