USCAO Section 1.2 Dual Palindromes

原题:
Dual Palindromes
A number that reads the same from right to left as when read from left to right is called a palindrome. The number 12321 is a palindrome; the number 77778 is not. Of course, palindromes have neither leading nor trailing zeroes, so 0220 is not a palindrome.

The number 21 (base 10) is not palindrome in base 10, but the number 21 (base 10) is, in fact, a palindrome in base 2 (10101).

Write a program that reads two numbers (expressed in base 10):

N (1 <= N <= 15)
S (0 < S < 10000)
and then finds and prints (in base 10) the first N numbers strictly greater than S that are palindromic when written in two or more number bases (2 <= base <= 10).
Solutions to this problem do not require manipulating integers larger than the standard 32 bits.
题意:
一个数字本身可能不是回文数,但是在其他进制下就可能是回文数了。输入两个数N,S,求出大于S的前N个在2-10进制中有两个或以上进制是回文数的数。
进制转换,没有什么技巧,直接上代码:

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

#include <iostream>
#include <fstream>
#include <string>
#include<algorithm>
using namespace std;
const int N=25;

char base[N];
char str[N*100];

void init(){
    for(int i=0;i<10;i++){
        base[i]=char(i+'0');
    }
    for(int i=10;i<=20;i++){
        base[i]=char('A'+i-10);
    }
//  cout<<base<<endl;
}

//进制转换函数 
int change(int t,int n){
    int r,num=0;
    while(t){
        r=t%n;
        t/=n;
        str[num++]=base[r];
    }   
    str[num]=0;
    return num; 
}

//回文数判定函数 
bool isOK(int size){
    for(int i=0;i<size/2;i++){
        if(str[i]!=str[size-i-1])
            return false;
    }   
    return true;
}

int main()
{
    ifstream fin("dualpal.in");
    ofstream fout("dualpal.out");
    init();
    int n,S,size;
    fin>>n>>S;
    int i=S+1;
    while(n){
        int count=0;
        for(int j=2;j<=10;j++){
            size=change(i,j);
            if(isOK(size)){
                count++;
            }
        }
        if(count>=2){
            n--;
            fout<<i<<endl;
        }
        i++;
    }
    fin.close();
    fout.close();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值