本文主要是介绍(判断数字类型)C - Petya and Java CodeForces - 66A,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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 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.
Examples
Input
127
Output
byte
Input
130
Output
short
Input
123456789101112131415161718192021222324
Output
BigInteger
题意: 判断数字类型,注意数字可能很大。
思路: 考察字符串模拟吧。如果能写大整数的话那那就这样吧。字符串模拟也可以。
实现过程中long范围的数字比较一直出问题,或许数字太大了就不能够比较大小了,总之很玄学。
注:没看到题目要求了是正数,是正数的话直接u long lnog就可以了
#include<iostream>
#include<stdio.h>
#include<map>
#include<cstring>
#include<algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <string>
using namespace std;
string str;
long long ans1;
int main()
{cin >> str;int len = (int)str.size();int i = 0;int minu = 0;if(str[0] == '-'){i = 1;minu = 1;}if(minu == 0){if(str > "9223372036854775807" || len > 19){printf("BigInteger");return 0;}}else if(minu == 1){if(str.substr(1,len) > "9223372036854775808" || len > 20){printf("BigInteger");return 0;}}for(;i < len;i++){ans1 = ans1 * 10 + str[i] - '0';}if(minu)ans1 = -ans1;if(ans1 >= -128 && ans1 <= 127){printf("byte");}else if(-32768 <= ans1 && ans1 <= 32767){printf("short\n");}else if(-2147483648 <= ans1 && ans1 <= 2147483647){printf("int");}else{printf("long");}}
//-9223372996854775808
//1234 567891011121314
这篇关于(判断数字类型)C - Petya and Java CodeForces - 66A的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!