• 1024 Palindromic Number


    A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers.

    Non-palindromic numbers can be paired with palindromic ones via a series of operations. First, the non-palindromic number is reversed and the result is added to the original number. If the result is not a palindromic number, this is repeated until it gives a palindromic number. For example, if we start from 67, we can obtain a palindromic number in 2 steps: 67 + 76 = 143, and 143 + 341 = 484.

    Given any positive integer N, you are supposed to find its paired palindromic number and the number of steps taken to find it.

    Input Specification:

    Each input file contains one test case. Each case consists of two positive numbers N and K, where N (≤1010) is the initial numer and K (≤100) is the maximum number of steps. The numbers are separated by a space.

    Output Specification:

    For each test case, output two numbers, one in each line. The first number is the paired palindromic number of N, and the second number is the number of steps taken to find the palindromic number. If the palindromic number is not found after K steps, just output the number obtained at the Kth step and K instead.


    Sample Input 1:

    67 3
    

    Sample Output 1:

    1. 484
    2. 2

    Sample Input 2:

    69 3
    

    Sample Output 2:

    1. 1353
    2. 3

    题目大意

    给你一个整数N,求N重复翻转相加多少次后,可以得到第一个回文数,结果输出最先得到的回文数与翻转次数。

    本题额外设置了最多翻转次数限制K,如果在翻转K次后还无法形成回文数,输出此时得到整数与K


    思路

    数组模拟运算,当然这题也可以当字符串来写


    C/C++ 

    1. #include
    2. using namespace std;
    3. vector<int> nums1,nums2;
    4. void OP1(); // 反转相加
    5. bool OP2(int head,int tail); // 回文数判断
    6. int main()
    7. {
    8. long long num,K;
    9. cin >> num >> K;
    10. do{
    11. nums1.push_back(num%10);
    12. num/=10;
    13. } while (num);
    14. for(int z=0;z
    15. if(!OP2(0,nums1.size()-1))OP1();
    16. else
    17. {
    18. K = z;
    19. break;
    20. }
    21. }
    22. for(int z=nums1.size()-1;z>=0;z--) cout << nums1[z];
    23. cout << endl << K << endl;
    24. return 0;
    25. }
    26. void OP1()
    27. {
    28. int len = nums1.size()-1;
    29. for(int z=0;z<=len;z++) nums2.push_back(nums1[z]+nums1[len-z]);
    30. for(int z=0;zif(nums2[z]>9){
    31. nums2[z] -= 10;
    32. nums2[z+1] += 1;
    33. }
    34. if(nums2[len]>9){
    35. nums2[len] -=10;
    36. nums2.push_back(1);
    37. }
    38. nums1.assign(nums2.begin(),nums2.end());
    39. nums2.clear();
    40. }
    41. bool OP2(int head,int tail)
    42. {
    43. if(head>=tail) return true;
    44. if(nums1[head]!=nums1[tail]) return false;
    45. return OP2(head+1,tail-1);
    46. }

  • 相关阅读:
    Oracle/PLSQL: Atan2 Function
    HCNP Routing&Switching之MUX VLAN
    智慧仓储数据可视化监控平台
    排序算法——直接插入排序
    plink分析100个性状的批量gwas分析
    【XSS跨站脚本】存储型XSS(持久型)
    Error: no matching distribution found for tensorflow-cpu==2.6.*
    详解ODX诊断数据库——ODX-V(整车网络拓扑)
    Windows11 安全中心页面不可用问题(无法打开病毒和威胁防护)解决方案汇总(图文介绍版)
    【Flutter】混合开发之Flutter预加载解决第一次加载页面缓慢问题
  • 原文地址:https://blog.csdn.net/daybreak_alonely/article/details/127707449