关闭

Section 1.2 Dual Palindromes

标签: c语言
100人阅读 评论(0) 收藏 举报
分类:

数制转换,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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2451次
    • 积分:167
    • 等级:
    • 排名:千里之外
    • 原创:14篇
    • 转载:0篇
    • 译文:1篇
    • 评论:0条