判断 ACdream 1202 Integer in C++

题目传送门

 /*
分情况讨论,在long long范围里可以直接比较
sscanf 直接读到n中去
*/
#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <vector>
using namespace std; const int MAXN = 1e4 + ;
const int INF = 0x3f3f3f3f; int main(void) //ACdream 1202 Integer in C++
{
//freopen ("G.in", "r", stdin); string s; string s_max = "";
while (cin >> s)
{
long long n;
int len = s.size ();
if (s[] != '-')
{
if (len < )
{
sscanf (s.c_str (), "%lld", &n);
if (n <= ) cout << "short" << endl;
else if (n <= ) cout << "int" << endl;
else cout << "long long" << endl;
}
else if (len == )
{
if (s > s_max) cout << "It is too big!" << endl;
else cout << "long long" << endl;
}
else cout << "It is too big!" << endl;
}
else
{
if (len < )
{
sscanf (s.c_str (), "%lld", &n);
if (n >= -) cout << "short" << endl;
else if (n >= ) cout << "int" << endl;
else cout << "long long" << endl;
}
else if (len == )
{
s.erase (s.begin ());
if (s < s_max) cout << "long long" << endl;
else cout << "It is too big!" << endl;
}
else cout << "It is too big!" << endl;
}
} return ;
} /*
-32768 to 32767
-2147483648 to 2147483647
-9223372036854775808 to 9223372036854775807
short, int, long long
It is too big!
*/
 /*
for循环保存n
注意:2147483647 + 1 -> -2147483648,-2147483648 > 2147483647
在二进制中并不是数学的比大小
*/
#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#include <vector>
using namespace std; const int MAXN = 1e4 + ;
const int INF = 0x3f3f3f3f; int main(void) //ACdream 1202 Integer in C++
{
freopen ("G.in", "r", stdin); string s;
char ss[], ss_ll1[] = "", ss_ll2[] = ""; while (scanf ("%s", &ss) == )
{
int len = strlen (ss); long long n;
if (len > ) puts ("It is too big!");
else if (ss[] == '-')
{
if (len == )
{
if (strcmp (ss+, ss_ll1) <= ) puts ("long long");
else puts ("It is too big!");
}
else if (len < )
{
n = ;
for (int i=; i<len; ++i) n = n * + (ss[i] - '');
n = -n;
if (n >= - && n <= ) puts ("short");
else if (n >= - && n <= ) puts ("int");
else puts ("long long");
}
}
else if (len <= )
{
if (len == )
{
if (strcmp (ss, ss_ll2) <= ) puts ("long long");
else puts ("It is too big!");
}
else
{
n = ;
for (int i=; i<len; ++i) n = n * + (ss[i] - '');
if (n >= - && n <= ) puts ("short");
else if (n <= && n >= -) puts ("int");
else puts ("long long");
}
}
else puts ("It is too big!");
} return ;
} /*
-32768 to 32767
-2147483648 to 2147483647
-9223372036854775808 to 9223372036854775807
short, int, long long
It is too big!
*/
上一篇:Linux相关问题总结


下一篇:Python中从SQL型数据库读写dataframe型数据