第二层第五题:双基回文数

Dual Palindromes
Mario Cruz (Colombia) & Hugo Rickeboer (Argentina)

A number that reads the same from right to left as when read from left toright is called a palindrome. The number 12321 is a palindrome; the number77778 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 Nnumbers strictly greater than S that are palindromic when written in two ormore number bases (2 <= base <= 10).

Solutions to this problem do not require manipulating integers larger thanthe standard 32 bits.

PROGRAM NAME: dualpal

INPUT FORMAT

A single line with space separated integers N and S.

SAMPLE INPUT (file dualpal.in)

3 25

OUTPUT FORMAT

N lines, each with a base 10 number that is palindromicwhen expressed in at least two of the bases 2..10. The numbers should be listedin order from smallest to largest.

SAMPLE OUTPUT (file dualpal.out)

26

27

28

 

 

       题目大意:如果一个数字在两个或更多进制下(2≤进制数≤10)均为回文数,则称该数为双基回文数。题目给出两个数字:S(0<S<10000)和N(1≤N≤15),求出大于S的连续N个双基回文数。

       枚举数字,然后枚举进制,然后转换进制,然后判断,然后输出,然后。。。AC

       代码如下:

/*
ID:su1q2d2
LANG:C++
TASK:dualpal
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#define maxl 50
#define lint long long
using namespace std;
void cn(char ans[],int x,int b)
{
	int i=0;
	while(x>0){
		ans[i]=x%b+'0';
		x/=b;
		i++;
	}
	ans[i]='\0';
	return ;
}
bool pal(char x[])
{
	int n;
	int i;
	n=strlen(x);
	for(i=0;i<n;i++){
		if(x[n-i-1]!=x[i]) return false;
	}
	return true;
}
int main()
{
	freopen("dualpal.in","r",stdin);
	freopen("dualpal.out","w",stdout);
	int i;
	int j;
	int c;
	int s;
	int n;
	int is;
	char afc[maxl];
	scanf("%d",&n);
	scanf("%d",&s);
	c=0;
	for(i=s+1;c<n;i++){
		is=0;
		for(j=2;j<=10;j++){
			cn(afc,i,j);
			if(pal(afc)) is++;
			if(is>=2) break;
		}
		if(is>=2){
			printf("%d\n",i);
			c++;
		}
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值