# Section 1.2 Dual Palindromes

178人阅读 评论(0)

/*
ID: alexyua2
PROG: dualpal
LANG: C++
*/

#include<fstream>
#include<string>
using namespace std;

ifstream fin("dualpal.in");
ofstream fout("dualpal.out");

//ifstream fin("in.txt");
//ofstream fout("out.txt");

bool IsDualPal(int);
bool IsPal(int,int);
int main()
{
int N,S;
fin>>N >>S;
while(N>0)
{
S++;
if(IsDualPal(S))
{
fout<<S <<endl;
N--;
}
}
fin.close();
fout.close();
return 0;
}

bool IsDualPal(int n)
{
/*
To judge whether n is
a dual-palindrom or not.
*/
int base;
int pal_num = 0;
for(base=2;base<=10;base++)
{
if(IsPal(n,base))
pal_num++;
if(pal_num == 2)
break;
}
if(pal_num == 2)
return true;
else
return false;
}

bool IsPal(int n,int base)
{
/*
To judge whether n is a
palindrom or not on the base given.
*/
string s;
int quotient,remainder;

do
{
quotient = n / base;
remainder = n % base;
n = quotient;
s.insert(s.begin(),remainder+'0');
}
while(quotient > 0);

int i,len=s.length();
bool result = true;
for(i=0;i<len/2;i++)
{
if(s[i] != s[len-i-1])
{
result = false;
break;
}
}
return result;
}
0
0

个人资料
• 访问：3052次
• 积分：173
• 等级：
• 排名：千里之外
• 原创：14篇
• 转载：0篇
• 译文：1篇
• 评论：0条
阅读排行