鱼C论坛

 找回密码
 立即注册
查看: 3561|回复: 2

leetcode26 溢出怎么解决

[复制链接]
发表于 2021-5-14 11:11:00 | 显示全部楼层 |阅读模式
15鱼币
  1. #include<stdio.h>
  2. #include<math.h>
  3. #include<stdlib.h>
  4.    int reverse(int x){
  5.    long zhuanhuan,ans=0;
  6.         while(x!=0)
  7.         {
  8.         zhuanhuan=x%10;
  9.         ans=ans*10+zhuanhuan;
  10.         x=x/10;
  11.     }
  12.     if(ans>2147483647||ans<-2147483648)
  13.         {
  14.         return -1;
  15.         }
  16.     else
  17.         return ans;

  18. }



  19. int main()
  20. {
  21.          int rev;
  22.          int n;
  23.          printf("please enter a number");
  24.          scanf("%d",&n);
  25.          rev=reverse(n);
  26.         if(rev==-1)
  27.         {
  28.                 printf("overflow");
  29.         }
  30.         else
  31.                 printf("%d is changing to %d",n,rev);
  32.          
  33. }
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-5-14 11:50:32 | 显示全部楼层
你可以把它变成字符串进行倒序
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-5-14 19:34:58 | 显示全部楼层
不争么 发表于 2021-5-14 11:50
你可以把它变成字符串进行倒序

这个题目就是整数倒序
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-29 10:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表