-
Time Limit:
- 10000ms Memory Limit:
- 65536kB
-
Description
-
如果一个正整数n至少在两个不同的进位制b1和b2下都是回文数(2<=b1,b2<=10< span="">),则称n是双基回文数(注意,回文数不能包含前导零)。输入十进制的正整数S<106,输出比S大的最小双基回文数(十进制)
Input
- 一个十进制整数 Output
- 一个十进制整数 Sample Input
-
1600000
Sample Output
-
1632995
http://nnsznoi.openjudge.cn/directlycalculatin/0004/
第一想法尝试用itoa,但是g++不支持。
因此需要自己实现itoa,参考http://stackoverflow.com/questions/228005/alternative-to-itoa-for-converting-integer-to-string-c
string itoa(int a)
{
string ss=""; //create empty string
while(a)
{
int x=a%10;
a/=10;
char i='0';
i=i+x;
ss=i+ss; //append new character at the front of the string!
}
return ss;
}
但发现运行的很慢,因此需要避免char相加,改进如下:
string myitoa(int a, int base)
{
string ss;
ss.clear();
while(a)
{
ss += (a%base);
a /= base;
}
return ss;
}
AC code:
#define RUN
#ifdef RUN
/**
http://nnsznoi.openjudge.cn/directlycalculatin/0004/
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <vector>
#include <list>
#include <cctype>
#include <algorithm>
#include <utility>
#include <math.h>
using namespace std;
#define LL long long
#define MAXN 1000001
char tmp[MAXN];
string myitoa(int a, int base)
{
string ss;
ss.clear();
while(a)
{
ss += (a%base);
a /= base;
}
return ss;
}
bool isPalindrome(string s){
int len = s.length();
for(int i=0; i<len/2; i++){
if(s[i] != s[len-1-i]){
return false;
}
}
return true;
}
void check(LL n){
LL test = n+1;
for(;;){
int cnt = 0;
for(int i=2; i<=10; i++){
//itoa(test, tmp, i);
string s = myitoa(test, i);
if(isPalindrome(s)){
//cout << test << "--" << s << "---" << cnt << endl;
cnt++;
if(cnt >= 2){
cout << test << endl;
return;
}
}
}
++test;
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("oj0004.in", "r", stdin);
freopen("oj0004.out", "w", stdout);
#endif
LL n;
while(scanf("%lld",&n) != EOF){
check(n);
}
}
#endif