-
Notifications
You must be signed in to change notification settings - Fork 285
Expand file tree
/
Copy pathAC_simulation_n.cpp
More file actions
60 lines (52 loc) · 1.31 KB
/
AC_simulation_n.cpp
File metadata and controls
60 lines (52 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_simulation_n.cpp
* Create Date: 2014-11-30 00:21:56
* Descripton: simulation
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
public:
int atoi(string str) {
int ret = 0;
bool overflow = false;
int sign = 1; // default is '+'
int i = 0;
int len = str.length();
while (i < len && (str[i] == ' ' || str[i] == '\n' || str[i] == '\t'))
++i;
if (i == len)
return 0;
// get sign
if (str[i] == '-') {
++i;
sign = -1;
} else if (str[i] == '+')
++i;
while (i < len) {
if (!isdigit(str[i]))
break;
if ((sign == 1 && ret > (INT_MAX - (str[i]-'0')) / 10) ||
(sign == -1 && -ret < (INT_MIN + (str[i]-'0')) / 10)) {
overflow = true;
break;
}
ret = ret * 10 + (str[i] - '0');
++i;
}
if (overflow)
ret = (sign == 1) ? INT_MAX : INT_MIN;
else
ret *= sign;
return ret;
}
};
int main() {
string str;
Solution a;
while (cin >> str)
cout << a.atoi(str) << endl;
return 0;
}