第几个回文数的问题

来源:百度知道 编辑:UC知道 时间:2024/05/16 06:00:23
输入一个数n,输出第n个回文数(如1,2,3,4,5,6,7,8,9,11,22,101,505……)
应该如何实现呢
eg
输入
12
24
输出
33
151

上面二位的程序的算法负责度是O(n×n),如果输入80000,基本上就死掉了,
我下面的程序的算法负责度给输出结果的长度相关,例如输出 34566543 的复杂度就是O(8),俺的程序即使输入 很大的int也立马就给出结果, 例如
输入: 4567898
输出:
4567898
the NO.4567898 coprimer num is 3567899987653

#pragma warning(disable:4786)
#include <iostream>
#include <string>
using namespace std;

const char *num1[] = {"0","1","2","3","4","5","6","7","8","9"};

string getCoprimerNum(int no)
{
int num = 9; //not include 0
int num2 = 10; //include 0
int size = 1;
int times = 0;
while(true)
{
if(no <= num) break;
no = no -num;
++size;
++times;
if(times == 2)
{
num *= 10;
num2 *= 10;
times = 0;
}
}
string str;
string str2;
bool zero = false;
num = num2;

--no; //change no star