@zsh-o
2018-09-19T09:39:45.000000Z
字数 751
阅读 1410
算法
#include <stdio.h>
#include <string.h>
const int INT_MAX = 2147483647;
const int INT_MIN = (-INT_MAX - 1);
const int MAXS = 1e5 + 1;
char S[MAXS];
int len;
int char2int(char c){
return (int)(c - '0');
}
int main(){
scanf("%s", S);
len = strlen(S);
int sum = 0;
bool negative = false;
bool overbound = false;
if(S[0] == '-'){
negative = true;
}
else if(S[0] <= '9' && S[0] >= '0'){
sum += char2int(S[0]);
}
for(int i=1; i< len; i++){
if(S[i] <= '9' && S[i] >= '0'){
int t = char2int(S[i]);
// 判断是否越界
if(negative == false){
if(sum > INT_MAX / 10){
overbound = true;
sum = INT_MAX;
break;
}
sum *= 10;
if(INT_MAX - sum < t){
overbound = true;
sum = INT_MAX;
break;
}
sum += t;
}else{
if(sum < INT_MIN / 10){
overbound = true;
printf("1-------\n");
sum = INT_MIN;
break;
}
sum *= 10;
if(INT_MIN - sum > -t){
overbound = true;
sum = INT_MIN;
break;
}
sum -= t;
}
}
else{
break;
}
}
printf("%d\n", sum);
}