• USACO Training 1.3 Duel Palindromes


    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.

    比如12321反过来读还是12321(所以是回文数),但77778就不是。

    Of course, palindromes have neither leading nor trailing zeroes, so 0220 is not a palindrome.

    但是0在首位或者个位都不算,所以0220不是一个回文数。

    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).

    给一个10进制数下的21,他不是一个回文数。但是2进制下的21(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.

    一共N航,每一行包含一个10进制数,至少两个进制们需要从小到大打印出来。

    SAMPLE OUTPUT (file dualpal.out)

    26
    27
    28

    1. /*
    2. ID: choiyin1
    3. PROG: dualpal
    4. LANG: C++
    5. */
    6. #include
    7. using namespace std;
    8. char leter[20]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J'};
    9. void changetheBase(int x,int base,char out[60])
    10. {
    11. stack<char> S;
    12. int i=0;
    13. while(x>0)
    14. {
    15. S.push(leter[x%base]);
    16. x/=base;
    17. }
    18. for(i=0;!S.empty();i++)
    19. {
    20. out[i]=S.top();
    21. S.pop();
    22. }
    23. out[i]=0;
    24. }
    25. bool isPalindromic(char input[60])
    26. {
    27. int temp[60];
    28. int len=strlen(input);
    29. for(int i=0;i
    30. {
    31. temp[len-i-1]=input[i];
    32. }
    33. for(int i=0;i
    34. {
    35. if(input[i]!=temp[i])
    36. return false;
    37. }
    38. return true;
    39. }
    40. bool islegal(int x)
    41. {
    42. char out[60];
    43. int ans=0;
    44. for(int i=2;i<=10;i++)
    45. {
    46. changetheBase(x,i,out);
    47. if(isPalindromic(out))
    48. {
    49. ans++;
    50. }
    51. if(ans>=2)
    52. return true;
    53. }
    54. return false;
    55. }
    56. int main()
    57. {
    58. int N,S,ans;
    59. freopen("dualpal.in","r",stdin);
    60. freopen("dualpal.out","w",stdout);
    61. scanf("%d%d",&N,&S);
    62. ans=0;
    63. for(int i=S+1;ans
    64. {
    65. if(islegal(i))
    66. {
    67. cout<
    68. ans++;
    69. }
    70. }
    71. }

  • 相关阅读:
    Python3操作文件系列(三):OpenPyXl模块三大对象操作Excel文件
    Linux中线程的介绍
    推荐算法高级案例-通过Wide&Deep算法进行特征组合的商品推荐详细教程 代码+数据
    体质计算小网页
    Spring 中的一些小知识点
    医疗保健行业的福音是对话式AI吗?
    Kubernetes学习篇之数据加密
    6.26CF模拟赛B:数组缩减题解
    医药行业数智化供应链管理系统:完善企业管理水平,驱动医药供应链协同发展
    ISIS默认层级实验简述
  • 原文地址:https://blog.csdn.net/GeekAlice/article/details/126928139