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

来源:互联网 发布:淘宝客服骗局 编辑:程序博客网 时间:2024/04/30 13:32

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:su1q2d2LANG:C++TASK:dualpal*/#include<iostream>#include<cstring>#include<cstdio>#define maxl 50#define lint long longusing 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;}

0 0
原创粉丝点击