zoukankan      html  css  js  c++  java
  • 1

    Reverse digits of an integer.
    Example1: x = 123, return 321
    Example2: x = -123, return -321
    Discuss: 1.If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
          2.Reversed integer overflow.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace ReverseInteger
    {
        class Program
        {
            /*Reverse digits of an integer.
                Example1: x = 123, return 321
                Example2: x = -123, return -321
                Discuss: 1.If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.
                      2.Reversed integer overflow.*/
            static void Main(string[] args)
            {
                //"-2147483648","2147483647",           
                int x = 2147483647;
                int y = Reverse(x);
            }
    
            public static int Reverse(int x)
            {
                int result = 0;
                while (x != 0)
                {
                    //if(y*10/10!=y) return 0;
                    if (result > int.MaxValue / 10)
                        return 0;
                    if (result < int.MinValue / 10)
                        return 0;
                    result = result * 10 + x % 10;
                    x /= 10;
                }
    
                return result;
            }
        }
    }
    View Code
  • 相关阅读:
    设计模式-代理模式
    设计模式-策略模式
    设计模式-单例模式
    优先队列
    n!中质因子个数
    计算组合数
    高精度
    memset用法
    质因子分解
    素数筛法
  • 原文地址:https://www.cnblogs.com/binyao/p/4992996.html
Copyright © 2011-2022 走看看