usaco——dualpal

 

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

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

 

我的代码:

/* ID: guangxue1 PROG: dualpal LANG: C++ */ #include <iostream> #include <fstream> #include <string> using namespace std; bool isPalindrom(string p) { for(int i=0; i<p.size()/2+1; i++) if(p[i]!=p[p.size()-i-1]) return false; return true; } string core(int a,int s) { string p; while(a/s!=0) { if(a%s>9) p+='A'+a%s-10; else p+='1'+a%s-1; a/=s; } if(a%s>9) p+='A'+a%s-10; else p+='1'+a%s-1; return p; } int main() { ofstream fout ("dualpal.out"); ifstream fin ("dualpal.in"); int a,b,t=0; fin>>a>>b; for(int i=b+1; t<a; i++) for(int j=2,tt=0; j<11; j++) if(isPalindrom(core(i,j))) { tt++;if(tt==2){t++,fout<<i<<endl;break;}} return 0; }

USACO Gateway  |   Comment or Question

你可能感兴趣的:(usaco——dualpal)