首页 > 其他 > 详细

1.2.5 Dual Palindromes

时间:2016-01-25 06:34:44      阅读:282      评论:0      收藏:0      [点我收藏+]

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.

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 palindromic when expressed in at least two of the bases 2..10. The numbers should be listed in order from smallest to largest.

SAMPLE OUTPUT (file dualpal.out)

26

27

28

 

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 int main()
 6 {
 7     freopen("dualpal.in","r",stdin);
 8     freopen("dualpal.out","w",stdout);
 9     int n,s;
10     int cnt=0;
11     char m[111];
12     cin>>n>>s;
13     for(int i=s+1;cnt!=n;i++){
14         int ans=0;
15         for(int k=2;k<=10;k++){
16             memset(m,0,sizeof(m));
17             int p=i;
18             int j=0;
19             while(p){
20                 m[j++]=p%k+0;
21                 p/=k;
22             }
23             int f=1;
24             for(int l=0,b=j-1;l<j;l++,b--){
25                 if(m[l]!=m[b]){
26                     f=0;
27                     break;
28                 }
29             }
30             if(f) ans++;
31         }
32         if(ans>=2) {cnt++;cout<<i<<endl;}
33     }
34     return 0;
35 }

 

1.2.5 Dual Palindromes

原文:http://www.cnblogs.com/shenyw/p/5156468.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!