【CodeForces】[66A]Petya and Java
来源:互联网 发布:php读取中文文件名 编辑:程序博客网 时间:2024/05/28 20:20
判断一个数的数据类型
除了BigInteger之外其它的都可以直接判断
所以主要是判断是否是BigInteger
可根据位数
那么只需要判断位数与边界值位数相同的情况
其余的就是一些小技巧了
#include<stdio.h>#include<string.h>int l;char s[120];char res[5][11]= {"byte","short","int","long","BigInteger"};bool cmp() { int x=(s[0]=='-'?1:0); if(l>19+x) return true; if(l<19+x) return false; char bg[20]="9223372036854775807"; if(x) bg[18]='8'; for(int i=x; i<l; i++) { if(s[i]>bg[i-x]) return true; if(s[i]<bg[i-x]) return false; } return false;}int judge() { if(cmp()) return 4; else { __int64 sum=0; int i=(s[0]=='-'?1:0); for(; i<l; i++) sum=sum*10+s[i]-'0'; if(s[0]=='-') sum--; if(sum<128) return 0; if(sum<32768) return 1; if(sum<=2147483647) return 2; else return 3; }}int main() { while(scanf("%s",s)!=EOF) { l=strlen(s); printf("%s\n",res[judge()]); } return 0;}
题目地址:【CodeForces】[66A]Petya and Java
题意:
给出一个数字,要求输出它的数据类型
- 128~127为byte
- 32768~32767为short
- 2147483648~2147483647为int
- 9223372036854775808~9223372036854775807为long
其它的为BigInteger
解题过程:
因为数字的位数较大
所以可用字符串读取
除了BigInteger之外其它的都在__int64范围内
所以如果可以判断数字在- 9223372036854775808~9223372036854775807
则可转换为数字用__int64储存再进行进一步的范围判断
所以主要是判断是否是BigInteger
可根据位数进行初步判断
可知
字符串长度大于19的正数(s[0]!=’-’)及大于20的负数(s[0]==’-’)
一定为BigInteger
字符串长度小于19的正数(s[0]!=’-’)及小于20的负数(s[0]==’-’)
一定不为BigInteger
所以只需要判断等于的情况
则可建立一 “9223372036854775807” 字符串
令其与字符串比较即可进行判断
0 0
- 【CodeForces】[66A]Petya and Java
- Codeforces 66A Petya and Java
- CodeForces 66A - Petya and Java(思维)
- Codeforces 66A:Petya and Java(水题)
- 【Codeforces】66A - Petya and Java(模拟)
- A. Petya and Java
- 【CodeForces】A. Petya and Strings
- CodeForce 66A Petya and Java
- cf 66A Petya and Java
- codeforces 112A Petya and Strings
- 【水String】#61 A. Petya and Java
- Problem--112A--Codeforces--A. Petya and Strings
- Codeforces 66E - Petya and Post
- 【CodeForces】[66B]Petya and Countryside
- CodeForces 66B - Petya and Countryside(水题)
- Petya and Staircases CodeForces
- Codeforces 112A-Petya and Strings(实现)
- CodeForces 576A - Vasya and Petya's Game
- 判断是否为平衡二叉树
- c++第五次作业
- unix环境高级编程(阅读笔记)------signal函数与sigaction的区别
- Qt:使用C++还是QML
- GBK与UTF-8的区别
- 【CodeForces】[66A]Petya and Java
- APP开发实战9-API接口安全方案
- Apache Flink:特性、概念、组件栈、架构及原理分析
- hdu——2084数塔
- 创建 jsp 时,网页出现莫名错误
- Maven简单了解
- APP开发实战10-APP数据同步方案
- Java中锁机制Synchronized方法说明 转自https://zm12.sm-tc.cn/?src=l4uLj8XQ0IiIiNGOjo2anpvRnJCS0JWeiZ7Qzc%2FOz9DP
- 改进的经度坐标校正鱼眼图像算法_code