zoukankan      html  css  js  c++  java
  • LeetCode 7

    Reverse Integer

    Reverse digits of an integer.

    Example1: x = 123, return 321
    Example2: x = -123, return -321

     1 /*************************************************************************
     2     > File Name: LeetCode007.c
     3     > Author: Juntaran 
     4     > Mail: Jacinthmail@gmail.com
     5     > Created Time: 2016年04月24日 星期日 17时03分19秒
     6  ************************************************************************/
     7 
     8 /*************************************************************************
     9 
    10     Reverse Integer
    11     
    12     Reverse digits of an integer.
    13 
    14     Example1: x = 123, return 321
    15     Example2: x = -123, return -321
    16 
    17  ************************************************************************/
    18  
    19 #include <stdio.h>
    20 #include <limits.h>
    21 
    22 int reverse(int x){
    23 
    24     long int result = 0;
    25 
    26     /* 取出数字 */
    27     int p = x>0 ? x : -x;
    28     
    29     /* Reverse */
    30     while( p != 0 ){
    31         result = result*10 + p%10;
    32         p = p/10;
    33     }
    34     
    35     /* 添加符号 */
    36     result = (x>0) ? result : -result;
    37     
    38     /* 判断溢出 */
    39     if( result > INT_MAX || result < INT_MIN ){
    40         return 0;
    41     }else{
    42         printf("%d
    ",result);
    43         return result;
    44     }
    45 }
    46 
    47 int reverse1(int x){
    48     
    49     long result = 0;
    50     while(x != 0){
    51         result = result * 10 + x % 10;
    52         x = x/10;
    53     }
    54     if( result > INT_MAX || result < INT_MIN ){
    55         return 0;
    56     }
    57     printf("%d
    ",result);
    58     return (int)result;
    59 }
    60 
    61 int main(){
    62     int x = -15984;
    63     reverse(x);
    64     reverse1(x);
    65 }
  • 相关阅读:
    对话框隐藏的简单实现(转)
    CoInitializeSecurity 学习(转)
    VC++实现CD/DVD刻录(转)
    网络连接的保活机制(心跳机制转)
    tcp架构
    木马的隐藏及其启动方式 (转)
    vs2005 控件bug
    vs2005 虚拟调试配置(转)
    sprintf用法(转)
    要掌握真正的免杀必须懂汇编【汇编语言超浓缩教程】(转)
  • 原文地址:https://www.cnblogs.com/Juntaran/p/5428764.html
Copyright © 2011-2022 走看看