1.2.4Palindromic Squares

  
    
  1. /*
  2. ID: awsd1231
  3. PROG: palsquare
  4. LANG: C++
  5. */
  6. #include<iostream>
  7. #include<cstdio>
  8. #include<cmath>
  9. #include<cstring>
  10. using namespace std;
  11. const char c[10] = {'A','B','C','D','E','F','G','H','I','J'};
  12. int B;
  13. char xB[20] = {0}, xbb[20] = {0};
  14. void toB(int xq) {
  15. int idx = 0;
  16. bool begin = true;
  17. for(int i = 20; i != -1; --i) {
  18. if(begin && pow(B, i) > xq) continue;//排除开头0
  19. else{
  20. begin = false;
  21. for(int j = B-1; j != -1; --j) {
  22. if(j * pow(B, i) <= xq) {
  23. xq -= j * pow(B, i);
  24. if(j < 10) xbb[idx++] = j + '0';
  25. else xbb[idx++] = c[j - 10];
  26. break;
  27. }
  28. }
  29. }
  30. }
  31. printf("%s ", xbb);
  32. }
  33. bool isPalindromic(int xq) {//输出x 和 B进制的x平方
  34. int idx = 0;
  35. bool begin = true;
  36. for(int i = 20; i != -1; --i) {
  37. if(begin && pow(B, i) > xq) continue;//排除开头0
  38. else{
  39. begin = false;
  40. for(int j = B-1; j != -1; --j) {
  41. if(j * pow(B, i) <= xq) {
  42. xq -= j * pow(B, i);
  43. if(j < 10)xB[idx++] = j + '0';
  44. else xB[idx++] = c[j - 10];
  45. break;
  46. }
  47. }
  48. }
  49. }
  50. int len = strlen(xB), t;
  51. for(int i = 0; i != len/2; ++i) {
  52. if(xB[i] != xB[len - i -1])
  53. return false;
  54. }
  55. return true;
  56. }
  57. int main() {
  58. freopen("palsquare.in", "r", stdin);
  59. freopen("palsquare.out", "w", stdout);
  60. cin >> B;
  61. for(int i = 1; i != 301; ++i)
  62. if(isPalindromic(i*i)) {
  63. toB(i);
  64. printf("%s\n", xB);
  65. }
  66. return 0;
  67. }





你可能感兴趣的:(ROM)