• 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. }

  • 相关阅读:
    Java集合-ArrayList源码分析
    飞桨(PaddlePaddle)数据加载教程
    ESP32外部中断原理详解及代码示例
    并查集知识梳理
    vue3源码分析——手写diff算法
    信息系统项目管理师0067:数据建模(5信息系统工程—5.2数据工程—5.2.1数据建模)
    split和join方法用于字符串和数组的转换
    JS-回到顶部
    【MindSpore】CPU可以正常运行的,但是GPU下报错
    Transformers基本组件(一)快速入门Pipeline、Tokenizer、Model
  • 原文地址:https://blog.csdn.net/GeekAlice/article/details/126928139