Njzy在对回文数的学习过程中发现了一个有趣的问题.
这个问题是求一个数的回文距离。一个数的回文距离的定义是它减去一个回文数的绝对值的最小值。
比如:121的回文距离就是0,因为|121-121|=0,123的回文距离是2,|123-121|=2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CSDN
{
class Program
{
static void Main(string[] args)
{
string input = null;
while ((input= Console.ReadLine())!=null)
{
string number = input;
GetDistance(number);
}
}
public static void GetDistance(string num)
{
int n = Convert.ToInt32(num);
while (true)
{
if (IsHuiWenShu(n))
{
break;
}
n--;
}
int dis = Math.Abs(Convert.ToInt32(num) - n);
Console.WriteLine(dis);
}
public static bool IsHuiWenShu(int n)
{
string s = Convert.ToString(n);
int lenght = s.Length;
int half = lenght / 2;
for (int i = 0; i < half; i++)
{
if (s[i] != s[lenght - i - 1])
{
return false;
}
}
return true;
}
}
}