Section 1.2 Dual Palindromes

数制转换,num2str,string方法
是上面一题(Palindromic Squares)的简单变形

/*
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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值