sscanf和sprintf的使用强化
- 1、sscanf和sprintf的使用强化
- 2、INT_MAX和INT_MIN两个宏的使用强化
class Solution {
public:
int reverse(int x) {
int tag=0;
long long num=x;//注意,一定要用long long承载,不然INT_MIN不好受
if( num<0 )
{
tag=1;
num*=(-1);
}
char solve[100];
sprintf(solve,"%lld",num);
int len=strlen( solve );
int L=0,R=len-1;
while( L<R )
{
solve[L]^=solve[R];
solve[R]^=solve[L];
solve[L]^=solve[R];
++L;
--R;
}
long long ret;
long long one=INT_MAX;
long long Two=INT_MIN;
sscanf(solve,"%lld",&ret);
//小于0
if( tag )
{
if( (long long )(-1)*ret<Two )
{
return 0;
}
else
{
return -1*(int)ret;
}
}
else
{
if( ret>one )
{
return 0;
}
else
{
return (int)ret;
}
}
}
};