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;
}
}
}