Mario Cruz (Colombia) & Hugo Rickeboer (Argentina)
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)
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
-------------------------------------------------------------------------------
/* ID: 551100k1 LANG: C++ TASK: dualpal */ #include<stdio.h> #include<string.h> #include<iostream> using namespace std; int check(string s){ int i; for(i=0;i<s.length();i++) if(s[i]!=s[s.length()-i-1]) return 0; return 1; } string change(int n,int x){ int i,k=n; string s; while(k){ s=char(k%x+'0')+s; k/=x; } return s; } int main(){ freopen("dualpal.in","r",stdin); freopen("dualpal.out","w",stdout); int add,n,m,i,j; scanf("%d%d",&n,&m); for(i=m+1;n;i++){ add=0; for(j=2;j<=10;j++) if(check(change(i,j))) add++; if(add>1){ n--,printf("%d\n",i); } } }
沒有留言:
張貼留言