Problem:
回文数

Problem Statement

整数nに最も近い回文数を求めよ．

なお，非負整数xが回文数であるとは，xを十進法で表現した文字列とそれを反転させた文字列が等しいことをいう．

例えば0,7,33,10301などは回文数であり，32,90,1010などは回文数でない．

Input

入力は以下の形式に従う．与えられる数は全て整数である．

n

Constraints

1≦n≦10^4

Output

nに最も近い回文数を出力せよ．

そのような数が複数ある場合は最も小さいものを出力せよ．

Sample Input 1

13

Output for the Sample Input 1

11

13に最も近い回文数は11である．

Sample Input 2

7447

Output for the Sample Input 2

7447

7447は回文数なのでそのまま出力すればよい．

Sample Input 3

106

Output for the Sample Input 3

101

106に最も近い回文数は111と101のふたつあるが，そのうち小さい方の101を出力する．

Solution in C# (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;

class Program {
	static void Main(string[] args) {
		new Calc().calc();
	}
	public class Calc {
		public Calc() { }
		public void calc() {
			string str = ReadLine<string>();
			int length = str.Length;
			int f = str.IntParse();
			if (length == 1||IsKaibun(f)) { Console.WriteLine(str);return; }
			string top = "", under = "", center = "";
			top = str.Substring(0, length / 2-1+length%2);
			under = top.Reverse().Docking();
			center = str.Substring(length / 2-1, (length % 2 == 0 ? 2 : 1));
			if (center.Length % 2 == 1||IsKaibun(center.IntParse())) {
				str = top + center + under;
			} else {
				int item = center.IntParse();
				int c_10 = int.Parse(center[0]+"");
				int up = Math.Abs(item - ((c_10 + 1) * 10 + c_10 + 1));
				int down = Math.Abs(item - ((c_10 - 1) * 10 + (c_10 - 1)));
				int ba = Math.Abs(item - (c_10 * 10 + c_10));
				if (up<down&&up<ba) {
					center = (item + up) + "";
				} else if (down<up&&down<ba) {
					center = (item - down) + "";
				} else {
					center = ba + "";
				}
			}
			str = top + center + under;
			int x = str.IntParse();
			int y = f < x ? f - (x - f) : f + (f - x);
			if (x>y&&IsKaibun(y)) {
				y.WriteLine();
				return;
			}
			x.WriteLine();
			return;
		}
		private bool IsKaibun(int x) {
			char[] str = x.ToString().ToCharArray();
			int length = str.Length;
			for (int i = 0; i < length/2; i++) {
				if (str[i] != str[length - 1 - i]) return false;
			}
			return true;
		}
	}

	public static T ReadLine<T>() {
		return (T)Convert.ChangeType(Console.ReadLine(), typeof(T));
	}
	public static T[] ReadLine<T>(params char[] separators) {
		return Console.ReadLine()
			.Split(separators)
			.Select(_ => (T)Convert.ChangeType(_, typeof(T)))
			.ToArray();
	}

	public static List<T> ReadLines<T>(int readCount) {
		List<T> rt = new List<T>();
		for (int i = 0; i < readCount; i++) {
			rt.Add(ReadLine<T>());
		}
		return rt;
	}
}
public static class Utils {
	public static string Docking<T>(this IEnumerable<T> s, int sequenceRange, Func<T, string> filter = null) {
		string str = "";
		int c = 0;
		foreach (var item in s) {
			str += filter == null ? item.ToString() : filter(item);
			c++;
			if (c == sequenceRange) break;
		}
		return str;
	}

	public static string Docking<T>(this IEnumerable<T> s, Func<T, string> filter = null) {
		return s.Docking(s.Count(), filter);
	}

	public static string RangeDocking<T>(this IEnumerable<T> s,int start,int end,Func<T,string> filter = null) {
		string str = "";
		end = end < s.Count() ? end : s.Count();
		var items = s.ToArray();
		for (int i = start; i < end; i++) {
			str+= filter == null ? items[i].ToString() : filter(items[i]);
		}
		return str;
	}
	public static int IntParse(this string n) {
		return int.Parse(n);
	}
	public static void WriteLine(this object obj) {
		Console.WriteLine(obj);
	}
}