public class Solution { public int Reverse(int x) { int fuhao = 1; if (x < 0) { fuhao = -1; } try { x = Math.Abs(x); } catch (Exception e) { return 0; } int i = 0; var list = new List<long>(); do { long num = x % Convert.ToInt64(Math.Pow(10, i + 1)) / Convert.ToInt64(Math.Pow(10, i)); list.Add(num); i++; } while (x / Convert.ToInt64(Math.Pow(10, i)) != 0); var length = list.Count; long result = 0; for (int j = 0; j < length; j++) { try { result += list[j] * Convert.ToInt64(Math.Pow(10, length - j - 1)); } catch (Exception e) { result = 0; break; } } result *= fuhao; int res = 0; int.TryParse(result.ToString(), out res); //Console.WriteLine(res); return res; } }
转载于:https://www.cnblogs.com/asenyang/p/6732725.html