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を出力する．

Submitted solutions:

code_snippet: #include "bits/stdc++.h"

using namespace std;

#define ll long long
#define REP(i, n) for(int i = 0;i < n; i ++)
#define REPR(i, n) for(int i = n;i >= 0; i--)
int main(){
    ll n, ans, dif = 100000000;
    cin >> n;
    REP(i, 1000000){
        string now = to_string(i);
        string e = now;
        reverse(e.begin(), e.end());
        if(now == e){
            if(dif > abs(n -i)){
                dif = abs(n - i);
                ans = i;
            }
        }
    }
    cout <<ans<<endl;
}
accuracy: 1
time_ms: 60
memory_kb: 3528
final_rank: 19

code_snippet: #include <iostream>
#include <vector>
using namespace std;

int main(){
    int n; cin>>n;
    int gmax = 1e9;
    int ans;
    for(int i=0; i<=100000; i++){
        vector<int> v;
        int num = i;

        while(num > 0){
            v.emplace_back(num % 10);
            num /= 10;
        }

        int m = v.size();
        bool valid = true;
        for(int j=0; j<m; j++){
            if(v[j] != v[m-1-j]) valid = false;
        }

        if(valid){
            if(abs(n - i) < gmax){
                gmax = abs(n - i);
                ans = i;
            }
        }
    }

    cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3164
final_rank: 11

code_snippet: #include <iostream>
#include <string>
using namespace std;

int main(){
    int n; cin>>n;
    int gmax = 1e9;
    int ans;
    for(int i=0; i<=100000; i++){
        string s;
        int num = i;

        while(num > 0){
            s += (num % 10) + '0';
            num /= 10;
        }

        int m = s.size();
        bool valid = true;
        for(int j=0; j<m; j++){
            if(s[j] != s[m-1-j]) valid = false;
        }

        if(valid){
            if(abs(n - i) < gmax){
                gmax = abs(n - i);
                ans = i;
            }
        }
    }

    cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3224
final_rank: 12

code_snippet: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
ld pi=acos(-1.0);

bool kai(int n) {
	string st(to_string(n));
	for (int i = 0; i < st.size(); ++i) {
		if (st[i] != st[st.size() - 1 - i]) {
			return false;
		}
	}
	return true;
}

int main() {
	int N;cin>>N;
	pair<int,int>ans(1000000,-1);
	for (int i = 1; i < 100000; ++i) {
		if (kai(i)) {
			pair<int,int>nans((abs(N-i)),i);
			ans=min(ans,nans);
		}
	}
	cout<<ans.second<<endl;
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3292
final_rank: 13

code_snippet: #include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
using ll = long long;

#define all(c) (c).begin(), (c).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pb(e) push_back(e)
#define mp(a, b) make_pair(a, b)
#define fr first
#define sc second

const ll INF = 1e9;
const ll MOD = 1e9 + 7;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

bool ok(string s) {
    auto l = s.substr(0, s.size() / 2);
    auto r = s.substr(s.size() / 2 + s.size() % 2, s.size() / 2);
    reverse(all(r));
    return l == r;
}
int main() {
    int n;
    cin >> n;
    int ans = INF;
    rep(i, 100005) {
        if (ok(to_string(i))) {
            if (abs(ans - n) > abs(i - n)) {
                ans = i;
            }
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 3248
final_rank: 17

code_snippet: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 10e9+9
#define EPS 1e-10
#define MAX_N 100100
#define MAX_M 100100

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int n;

string changes(int i){
	string res;
	stringstream ss;
	ss << i;
	ss >> res;
	return res;
}

int main(){
	cin >> n;
	int ans = INF;
	REP(i, 11000){
		bool f = 1;
		string s = changes(i);
		REP(i, s.size() / 2){
			if (s[i] != s[s.size() - 1 - i])f = 0;
		}
		if (f){
			if (abs(n - i) < abs(n - ans)){
				ans = i;
			}
		}
	}
	cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 1224
final_rank: 4

code_snippet: #include <string>
#include <iostream>
#include <algorithm>

using namespace std;

bool palindrome(int n)
{
    string S = to_string(n);
    
    string T = S; reverse(T.begin(), T.end());
    
    return S == T;
}

int main()
{
    int n, ret = 999999999;
    
    scanf("%d", &n);
    
    for(int i = 1; i <= 100001; i++)
    {
        if(palindrome(i) && abs(i - n) < abs(ret - n))
        {
            ret = i;
        }
    }
    
    printf("%d\n", ret);
    
    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3352
final_rank: 18

code_snippet: #include<string>
#include<vector>
#include<algorithm>
#include<iostream>
#include<sstream>
using namespace std;
bool latte(int n){
    stringstream ss;
    ss<<n;
    string str;
    ss>>str;
    string rev=str;
    reverse(rev.begin(),rev.end());
    return str==rev;
}
 int intAbs(int a){
    if(a<0)a*=-1;
    return a;
}
int main(){
    int n;
    cin>>n;
    int Min=10000000,MinNum;
    for(int i=0;i<10001;i++){
        if(!latte(i))continue;
        if(intAbs(n-i)<Min){
            Min=intAbs(n-i);
            MinNum=i;
        }
    }
    cout<<MinNum<<endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1228
final_rank: 9

code_snippet: #include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>

#define N 10000

using namespace std;

string convert_string(int n)
{
  stringstream ss;
  ss << n;
  return ss.str();
}

bool check(string str)
{
  int len = str.size();
  if(len == 1) return true;

  bool flag = true;
  for(int i = 0, i_len = len/2; i < i_len && flag; i += 1) {
    flag &= (str[i] == str[len-1-i]);
  }

  return flag;
}

int solve(int n)
{
  string str;
  int result = 0;

  for(int i = 1; i <= N; i += 1){
    str = convert_string(i);

    if(check(str) && abs(result - n) > abs(i - n)){
      result = i;
    }
  }

  return result;
}

int main()
{
  int n;
  cin>> n;

  cout << solve(n) << endl;

  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1216
final_rank: 2

code_snippet: #include <iostream>
#include <sstream>
#include <string>
#include <cstdlib>

using namespace std;

string convert_string(int n)
{
  stringstream ss;
  ss << n;
  return ss.str();
}

bool check(string str)
{
  int len = str.size();
  if(len == 1) return true;

  bool flag = true;
  for(int i = 0, i_len = len/2; i < i_len && flag; i += 1) {
    flag &= (str[i] == str[len-1-i]);
  }

  return flag;
}

int solve(int n)
{
  string str;
  int result = 0;

  for(int i = 1; i <= n; i += 1){
    str = convert_string(i);

    if(check(str) && abs(result - n) > abs(i - n)){
      result = i;
    }
  }

  return result;
}

int main()
{
  int n;
  cin>> n;

  cout << solve(n) << endl;

  return 0;
}
accuracy: 0.05714285714285714
time_ms: 10
memory_kb: 1216
final_rank: 20

code_snippet: #include<iostream>
#include<algorithm>
#include<string>
using namespace std;


string to_s(int n){
	string s;
	do{s+='0'+n%10,n/=10;}while(n);
	reverse(s.begin(),s.end());
	return s;
}



int main(){
	int n,ans=1<<30;
	cin>>n;
	for(int i=0;i<2*n;i++){
		string s(to_s(i));
		string t(s.begin(),s.end());
		reverse(t.begin(),t.end());
		if(s==t && abs(i-n)<abs(ans-n)){
			ans=i;
		}
	}
	cout<<ans<<endl;
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1212
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define for_(i,a,b) for(int i=a; i<b; ++i)
#define for_rev(i,a,b) for(int i=a;i>=b;--i)
#define allof(a) a.begin(),a.end()
#define minit(a,b) memset(a,b,sizeof(a))
#define size_of(a) (int)a.size()

int main() {
	int n; cin >> n;
	int sub = (int)1e9;
	int ans = -1;
	
	for_(i,0,10*n) {
		stringstream ss;
		ss << i;
		
		string s = ss.str();
		string sr = s;
		reverse(allof(sr));
		
		if (s == sr) {
			if (sub > abs(n - i)) {
				sub = abs(n - i);
				ans = i;
			}
		}
	}
	
	cout << ans << endl;
	
	return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 1224
final_rank: 16

code_snippet: #include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>


#include <cstdlib>
#include <sstream>
#include <climits>
#include <complex>

using namespace std;

#define MP make_pair

#define REP(i,n) for(int i=0;i<n;i++)

typedef long long LL;
typedef pair<int,int> PII;

bool is_palid[100000];

int main() {
  REP (i, 10001) {
    stringstream ss;
    ss << i;
    string s = ss.str();
    reverse(s.begin(), s.end());
    if (ss.str() == s) {
      is_palid[i] = true;
    }
  }

  int n;
  cin >> n;

  int up;
  int down;
  REP (i, 10001) {
    if (i <= n && is_palid[i]) {
      down = i;
    }
    if (10000 - i >= n && is_palid[10000 - i]) {
      up = 10000 - i;
    }
  }
  if (n - down <= up - n) {cout << down << endl;}
  else {cout << up << endl;}
}
accuracy: 1
time_ms: 10
memory_kb: 1228
final_rank: 5

code_snippet: #include <cstdio>
#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <string.h>
#include <vector>
#include <queue>
using namespace std;

bool isPalindrome(int k) {
	stringstream ss;
	ss << k;
	string s(ss.str());
	int n = s.length();
	for (int i = 0; i < n/2; i++) {
		if (s[i] != s[n-i-1])
			return false;
	}
	return true;
}

int main() {
	int n;
	cin >> n;
	int min = 10000000, res = 0;
	for (int i = 0; i < 100000; i++) {
		if (isPalindrome(i) and abs(n-i) < min) {
			min = abs(n-i);
			res = i;
		}
	}
	cout << res << endl;
}
accuracy: 1
time_ms: 60
memory_kb: 1216
final_rank: 14

code_snippet: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
  
using namespace std;
  
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
  
static const double EPS = 1e-8;

static const int tx[] = {0,1,0,-1};
static const int ty[] = {-1,0,1,0};

int main(){
  vector<int> palindrome;
  for(int i=1;i<=10000;i++){
    stringstream ss;
    ss << i;
    string str;
    string r_str;
    ss >> str;

    r_str = str;
    reverse(r_str.begin(),r_str.end());


    if(str == r_str){
      palindrome.push_back(i);
    }
  }

  sort(palindrome.begin(),palindrome.end());
  int num;
  while(~scanf("%d",&num)){
    int idx = lower_bound(palindrome.begin(),palindrome.end(),num) - palindrome.begin();

    int cand[3] = {INF,INF,INF};

    if(idx - 1 >= 0) cand[0] = palindrome[idx-1]; 
    cand[1] = palindrome[idx]; 
    if(idx + 1 < palindrome.size()) cand[2] = palindrome[idx+1];

    int diff = INF;
    int res = INF;
    for(int i=0;i<3;i++){
      if(abs(cand[i] - num) < diff){
	res = cand[i];
	diff = abs(cand[i] - num);
      }
      else if(abs(cand[i] - num) == diff){
	res = min(res,cand[i]);
      }
    }
    printf("%d\n",res);
  }
}
accuracy: 1
time_ms: 10
memory_kb: 1292
final_rank: 8

code_snippet: #include <iostream>
#include <sstream>
#include <string.h>

using namespace std;

void solve()
{
	bool isPalinNum[10010];
	memset(isPalinNum, 0, sizeof(isPalinNum));
	for(int i = 1; i < 10010; ++i)
	{
		stringstream ss;
		ss << i;
		bool flag = true;
		string s = ss.str();
		for(int j = 0; j < s.size() / 2; ++j)
		{
			if(s[j] != s[s.size() - j - 1])
			{
				flag = false;
				break;
			}
		}
		if(flag)
		{
			isPalinNum[i] = true;
		}
	}
	int n;
	cin >> n;
	if(isPalinNum[n])
	{
		cout << n << endl;
	}
	else
	{
		int ans_cand[2] = {0};
		for(int i = n - 1; i >= 0; --i)
		{
			if(isPalinNum[i])
			{
				ans_cand[0] = i;
				break;
			}
		}
		for(int i = n + 1; i < 10010; ++i)
		{
			if(isPalinNum[i])
			{
				ans_cand[1] = i;
				break;
			}
		}
		if(n - ans_cand[0] <= ans_cand[1] - n)
		{
			cout << ans_cand[0] << endl;
		}
		else
		{
			cout << ans_cand[1] << endl;
		}
	}
}

int main()
{
	solve();
	return(0);
}
accuracy: 1
time_ms: 10
memory_kb: 1236
final_rank: 6

code_snippet: #include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef pair<int,int> pint;

#define DE 1
#define FI first
#define SE second
#define PB push_back
#define MP make_pair
#define ALL(s) (s).begin(),(s).end()
#define REP(i,n) for (int i = 0; i < (int)(n); ++i)
#define EACH(i,s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl

template<class T1, class T2> ostream& operator<<(ostream &s, pair<T1,T2> P){return s<<'<'<<P.first<<", "<<P.second<<'>';}
template<class T> ostream& operator<<(ostream &s, vector<T> P) {s<<"{ ";for(int i=0;i<P.size();++i){if(i>0)s<<", ";s<<P[i];}return s<<" }"<<endl;}
template<class T1, class T2> ostream& operator<<(ostream &s, map<T1,T2> P) {s<<"{ ";for(__typeof__(P.begin()) it=P.begin();it!=P.end();++it){if(it!=P.begin())s<<", ";s<<'<'<<it->first<<"->"<<it->second<<'>';}return s<<" }"<<endl;}



int n;

bool IsPalNum(int n) {
    stringstream ss;
    ss << n;
    string s = ss.str();
    reverse(ALL(s));
    if (ss.str() == s) return true;
    else return false;
}

int main() {
    cin >> n;
    int Min = 100000, res = -1;
    for (int i = 1; i <= 11111; ++i) {
        if (IsPalNum(i) && Min > abs(i-n)) {
            Min = abs(i-n);
            res = i;
        }
    }
    cout << res << endl;
            
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1220
final_rank: 3

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<sstream>
#define all(c) (c).begin(),(c).end()

using namespace std;

string toStr(int n){
  stringstream ss;
  ss << n;
  return ss.str();
}

int main(void){
	
	vector<int>pnum;
	
	for(int i=0;i<20001;i++){
		string s=toStr(i);
		string r=s;
		reverse(all(r));
		if(s==r)pnum.push_back(i);
	}
	
	int n;
	while(cin >> n){
		vector<int>::iterator l;
		l=lower_bound(all(pnum),n);
	
		if(abs(n-*(l-1))>abs(n-*l))cout << *l << endl;
		else cout << *(l-1) << endl;
	}
	
	return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 1252
final_rank: 10

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<map>
#include<sstream>
#define pb push_back
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef pair<int,int> P;
bool check(int n){
  ostringstream os;
  os << n;
  string s=os.str();
  for(int i=0;i<s.size();i++){
    if(s[i]!=s[s.size()-i-1]){
      return false;
    }
  }
  return true;
}
int main(){
  int N=10000;
  vector<int> pn;
  rep(i,N+1){
    if(check(i))
    pn.pb(i);
  }
  int n;
  while(cin>>n){
    int ans=0;
    for(int i=0;i<pn.size();i++){
      if(abs(pn[i]-n)<abs(ans-n)){
	ans=pn[i];
      }
    }
    cout<<ans<<endl;

  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1240
final_rank: 7

code_snippet: #include <iostream>
#include <cstdio>
#include <vector>
#include <list>
#include <cmath>
#include <fstream>
#include <algorithm>
#include <sstream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <cmath>

using namespace std;
typedef long long ll;

bool isPali(ll N){
  stringstream ss;
  ss<<N;
  string s=ss.str();
  for(int i=0;i<(int)s.size();i++)
    if(s[i]!=s[(int)s.size()-1-i])
      return false;
  return true;
}

int main(){
  ll N;
  cin>>N;
  ll res=-1;
  for(int i=1;i<=100000;i++){
    if(isPali(i)){
      if(abs(res-N)>abs(i-N)){
        res=i;
      }
    }
  }
  cout<<res<<endl;
  

  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 1216
final_rank: 14