【codeforces】- Petya and Java(给不同的数,判断大小,选择不同数据类型)
2016-05-21 11:44
513 查看
A. Petya and Java
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 to9223372036854775807
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 n can'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 usecout (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.
Examples
input
output
input
output
input
output
用字符串把大数单独拿出来判断,其他的就可以直接和临界值比较判断类型。有一点,是负数是要注意位数
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
char a[111];
int l;
while (~scanf("%s",a))
{
l = strlen(a);
if (a[0]!='-')
{
if (l>19)
printf("BigInteger\n");
else if(l==19)
{
int t=strcmp(a,"9223372036854775807");
if(t==-1||t==0)
printf("long\n");
else
printf("BigInteger\n");
}
else if(l>=11&&l<19)
printf("long\n");
else
{
__int64 n=a[0]-'0';
for (int i=1;i<l;i++)
n=n*10+(a[i]-'0');
if (n>=0&&n<=127)
printf("byte\n");
else if(n>=128&&n<=32767)
printf("short\n");
else if(n>=32768&&n<=2147483647)
printf("int\n");
else
printf("long\n");
}
}
else
{
l--;
if (l>19)
printf("BigInteger\n");
else if(l>=11&&l<19)
printf("long\n");
else if(l==19)
{
int t=strcmp(a,"-9223372036854775808");
if(t==-1||t==0)
printf("long\n");
else
printf("BigInteger\n");
}
else
{
__int64 n=a[1]-'0';
for(int i=2;i<=l;i++)
n=n*10+(a[i]-'0');
if(n>=0&&n<=128)
printf("byte\n");
else if(n>=129&&n<=32768)
printf("short\n");
else if(n>=32769&&n<=2147483648)
printf("int\n");
else
printf("long\n");
}
}
}
return 0;
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 to9223372036854775807
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 n can'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 usecout (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.
Examples
input
127
output
byte
input
130
output
short
input
123456789101112131415161718192021222324
output
BigInteger
用字符串把大数单独拿出来判断,其他的就可以直接和临界值比较判断类型。有一点,是负数是要注意位数
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
char a[111];
int l;
while (~scanf("%s",a))
{
l = strlen(a);
if (a[0]!='-')
{
if (l>19)
printf("BigInteger\n");
else if(l==19)
{
int t=strcmp(a,"9223372036854775807");
if(t==-1||t==0)
printf("long\n");
else
printf("BigInteger\n");
}
else if(l>=11&&l<19)
printf("long\n");
else
{
__int64 n=a[0]-'0';
for (int i=1;i<l;i++)
n=n*10+(a[i]-'0');
if (n>=0&&n<=127)
printf("byte\n");
else if(n>=128&&n<=32767)
printf("short\n");
else if(n>=32768&&n<=2147483647)
printf("int\n");
else
printf("long\n");
}
}
else
{
l--;
if (l>19)
printf("BigInteger\n");
else if(l>=11&&l<19)
printf("long\n");
else if(l==19)
{
int t=strcmp(a,"-9223372036854775808");
if(t==-1||t==0)
printf("long\n");
else
printf("BigInteger\n");
}
else
{
__int64 n=a[1]-'0';
for(int i=2;i<=l;i++)
n=n*10+(a[i]-'0');
if(n>=0&&n<=128)
printf("byte\n");
else if(n>=129&&n<=32768)
printf("short\n");
else if(n>=32769&&n<=2147483648)
printf("int\n");
else
printf("long\n");
}
}
}
return 0;
}
相关文章推荐
- JAVA多线程之中断机制(stop()、interrupted()、isInterrupted())
- Java多线程(九)之ReentrantLock与Condition
- Context initialization failed org.springframework.beans.factory.BeanDefinitionStoreException: IOExc
- java 大数操作(加减乘除及取余)
- jni调用java数组导致VM aborting,安卓程序莫名闪退
- 关于:[Struts2]Caused by: com.opensymphony.xwork2.inject.DependencyException:
- spring-mybatis整合测试
- java遍历给定目录,树形结构输出所有文件,包括子目录中的文件
- Java静态的应用
- android studio 与eclipse创建命名空间时候的区别
- hdu 1042 N! java大数及判断文件末尾
- Java 23种设计模式之抽象工厂模式详解
- java中代码执行顺序
- java 杭电1058Humble Numbers
- SpringMVC接收ajax传送的参数,后台显示乱码
- Java基础第四天
- 消息模式Toast.makeText的几种常见用法
- 消息模式Toast.makeText的几种常见用法
- java实现双向链表
- 记忆化搜索路径_Java实现