输入:123 输出321
给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。若翻转后超出取值范围,则输出0即可输入:123 输出321输入:-123 输出-321
#include <stdio.h>
int main(int argc, char const *argv[])
{
printf("请输入一个32 位的有符号整数:");
int num, ge, shi, bai;
int fan;
scanf("%d", &num);
ge = num % 10;
shi = num / 10 % 10;
bai = num / 10 / 10;
while (num >= -2 ^ 31 || num <= 2 ^ 31 - 1)
{
if (num > 0)
{
fan = ge * 100 + shi * 10 + bai;
break;
}
else if (num < 0)
{
fan = ~(ge * 100 + shi * 10 + bai) * (-1) - 1;
break;
}
else if (num == 0)
{
fan = 0;
break;
}
}
if (num < -2 ^ 31 || num > 2 ^ 31 - 1)
{
fan = 0;
}
printf("反转后的结果为:%d\n", fan);
return 0;
}