Petya and Java
2016-05-16 21:53
555 查看
Description
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.
Input
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).
Output
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.
Sample Input
Input
Output
Input
Output
Input
Output
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.
Input
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).
Output
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.
Sample Input
Input
127
Output
byte
Input
130
Output
short
Input
123456789101112131415161718192021222324
Output
BigInteger
题意:
输入一个数字,判断其所属的类型。此题用字符串比较较为简单,用strcmp函数比较字符串大小。
代码如下:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int main() { char a[101]; scanf("%s",a); int l=strlen(a); if(l<3||(l==3&&strcmp(a,"127")<=0)) { printf("byte\n"); } else if(l<5||(l==5&&strcmp(a,"32767")<=0)) { printf("short\n"); } else if(l<10||l==10&&strcmp(a,"2147483647")<=0) { printf("int\n"); } else if(l<19||(l==19&&strcmp(a,"9223372036854775807")<=0)) { printf("long\n"); } else printf("BigInteger\n"); }
相关文章推荐
- 【Java】容器集合类
- java学习总结(16.05.16)java的标识符和命名规则
- [javaSE] 数组(获取最值)
- Java再学习-简单回调函数
- java基础第八天
- Java基础-第7天
- eclipse导入JDK源码
- spring 引入命名空间简介
- spring 数据连接泄漏
- spring数据连接泄漏
- Java面向对象之继承
- Java面向对象之多态
- 深入java之java语言基础(一)
- Java面向对象之多态
- Java中的反射机制复习
- spark学习11之在idea中将eclipse导入的java project改成maven project
- [疯狂Java]NIO.2:文件属性
- spring 获取 bean
- android开发使用eclipse遇到的问题
- java之StringTokenizer