码迷,mamicode.com
首页 > 编程语言 > 详细

判断 ACdream 1202 Integer in C++

时间:2015-04-10 22:21:20      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

 

题目传送门

 1 /*
 2     分情况讨论,在long long范围里可以直接比较
 3     sscanf 直接读到n中去
 4 */
 5 #include <cstdio>
 6 #include <iostream>
 7 #include <string>
 8 #include <algorithm>
 9 #include <cstring>
10 #include <map>
11 #include <set>
12 #include <vector>
13 using namespace std;
14 
15 const int MAXN = 1e4 + 10;
16 const int INF = 0x3f3f3f3f;
17 
18 int main(void)        //ACdream 1202 Integer in C++
19 {
20     //freopen ("G.in", "r", stdin);
21 
22     string s;    string s_max = "9223372036854775807";
23     while (cin >> s)
24     {
25         long long n;
26         int len = s.size ();
27         if (s[0] != -)
28         {
29             if (len < 19)
30             {    
31                 sscanf (s.c_str (), "%lld", &n);
32                 if (n <= 32767)    cout << "short" << endl;
33                 else if (n <= 2147483647)    cout << "int" << endl;
34                 else    cout << "long long" << endl;
35             }
36             else if (len == 19)
37             {
38                 if (s > s_max)    cout << "It is too big!" << endl;
39                 else cout << "long long" << endl;
40             }
41             else    cout << "It is too big!" << endl;
42         }
43         else
44         {
45             if (len < 20)
46             {
47                 sscanf (s.c_str (), "%lld", &n);
48                 if (n >= -32768)    cout << "short" << endl;
49                 else if (n >= 2147483647)    cout << "int" << endl;
50                 else    cout << "long long" << endl;
51             }
52             else if (len == 20)
53             {
54                 s.erase (s.begin ());
55                 if (s < s_max)    cout << "long long" << endl;
56                 else    cout << "It is too big!" << endl;
57             }
58             else    cout << "It is too big!" << endl;
59         }
60     }
61 
62     return 0;
63 }
64 
65 /*
66 -32768 to 32767
67 -2147483648 to 2147483647
68 -9223372036854775808 to 9223372036854775807
69 short, int, long long
70 It is too big!
71 */
 1 /*
 2     for循环保存n
 3     注意:2147483647 + 1 -> -2147483648,-2147483648 > 2147483647
 4         在二进制中并不是数学的比大小
 5 */
 6 #include <cstdio>
 7 #include <iostream>
 8 #include <string>
 9 #include <algorithm>
10 #include <cstring>
11 #include <map>
12 #include <set>
13 #include <vector>
14 using namespace std;
15 
16 const int MAXN = 1e4 + 10;
17 const int INF = 0x3f3f3f3f;
18 
19 int main(void)        //ACdream 1202 Integer in C++
20 {
21     freopen ("G.in", "r", stdin);
22 
23     string s;
24     char ss[33], ss_ll1[] = "9223372036854775808", ss_ll2[] = "9223372036854775807";
25 
26     while (scanf ("%s", &ss) == 1)
27     {
28         int len = strlen (ss);    long long n;
29         if (len > 20)    puts ("It is too big!");
30         else if (ss[0] == -)
31         {
32             if (len == 20)
33             {
34                 if (strcmp (ss+1, ss_ll1) <= 0)    puts ("long long");
35                 else    puts ("It is too big!");
36             }
37             else if (len < 20)
38             {
39                 n = 0;
40                 for (int i=1; i<len; ++i)    n = n * 10 + (ss[i] - 0);
41                 n = -n;
42                 if (n >= -32768 && n <= 32767)    puts ("short");
43                 else if (n >= -2147483648 && n <= 2147483647)    puts ("int");
44                 else    puts ("long long");
45             }
46         }
47         else if (len <= 19)
48         {
49             if (len == 19)
50             {
51                 if (strcmp (ss, ss_ll2) <= 0)    puts ("long long");
52                 else    puts ("It is too big!");
53             }
54             else
55             {
56                 n = 0;
57                 for (int i=0; i<len; ++i)    n = n * 10 + (ss[i] - 0);
58                 if (n >= -32768 && n <= 32767)    puts ("short");
59                 else if (n <= 2147483647 && n >= -2147483648)    puts ("int");
60                 else    puts ("long long");
61             }
62         }
63         else    puts ("It is too big!");
64     }
65 
66     return 0;
67 }
68 
69 /*
70 -32768 to 32767
71 -2147483648 to 2147483647
72 -9223372036854775808 to 9223372036854775807
73 short, int, long long
74 It is too big!
75 */

 

判断 ACdream 1202 Integer in C++

标签:

原文地址:http://www.cnblogs.com/Running-Time/p/4415733.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!