CodeForce 66A Petya and Java
2016-05-12 11:48
393 查看
A - Petya and JavaTime Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d& %I64uSubmit StatusDescriptionLittle 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 thatit 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 ifone 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 1272) short occupies 2 bytes and allows you to store numbers from - 32768 to 327673) int occupies 4 bytes and allows you to store numbers from - 2147483648 to 21474836474) long occupies 8 bytes and allows you to store numbers from - 9223372036854775808 to 92233720368547758075) 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.InputThe 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'tbe 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).OutputPrint 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 InputInput
127Output
byteInput
130Output
shortInput
123456789101112131415161718192021222324Output
BigInteger
题意:输入一个正数判断是什么类型的数。
思路:很明显要用到字符串知识,这个很容易想到,不过刚开始本想把字符转换为数字再进行比较,不过这样特别麻烦,
换个思路,如果利用字符串中比较大小的函数strcmp,明显简单多了。
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int main() { char s[110],n,m,i,j,k,l; while(scanf("%s",s)!=EOF) { l=strlen(s); if(l<3||l==3&&strcmp(s,"127")<=0) printf("byte\n"); else if(l<5||l==5&&strcmp(s,"32767")<=0) printf("short\n"); else if(l<10||l==10&&strcmp(s,"2147483647")<=0) printf("int\n"); else if(l<19||l==19&&strcmp(s,"9223372036854775807")<=0) printf("long\n"); else printf("BigInteger\n"); } return 0; }
相关文章推荐
- java线程深度解析(六)——线程池技术
- 认证OAuth - ScribeJava
- Shiro权限标签
- 【DAY2】Java的数据类型
- java实现堆的操作(建堆,插入,删除)
- Struts2处理流程性需求的一种解决方案
- Struts2处理流程性需求的一种解决方案
- java 知识 链接
- [Gradle]Error:java.lang.OutOfMemoryError: Java heap space . Please assign more memory to Gradle in t
- java实现将字符串转化为二进制输出
- java中ArrayList简单用法
- Struts2自定义标签——通过自定义标签实现权限控制
- Java中的NIO和IO的对比分析
- Java 注解指导手册 – 终极向导
- 【DAY1】Java的运行机制
- Java 工程师成神之路
- Rxjava 自学笔记(希望能帮到想学的小伙伴)在此感谢 jack 大神们的开源贡献精神
- java中,去除空白的方法
- Java注解教程及自定义注解
- JAVA中的观察者模式实例教程