zoukankan      html  css  js  c++  java
  • PAT_A1136#A Delayed Palindrome

    Source:

    PAT_A1136 A Delayed Palindrome (20 分)

    Description:

    Consider a positive integer N written in standard notation with k+1 digits ai​​ as ak​​a1​​a0​​ with 0 for all i and ak​​>0. Then N is palindromic if and only if ai​​=aki​​ for all i. Zero is written 0 and is also palindromic by definition.

    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. Such number is called a delayed palindrome. (Quoted from https://en.wikipedia.org/wiki/Palindromic_number )

    Given any positive integer, you are supposed to find its paired palindromic number.

    Input Specification:

    Each input file contains one test case which gives a positive integer no more than 1000 digits.

    Output Specification:

    For each test case, print line by line the process of finding the palindromic number. The format of each line is the following:

    A + B = C
    

    where A is the original number, B is the reversed A, and C is their sum. A starts being the input number, and this process ends until C becomes a palindromic number -- in this case we print in the last line C is a palindromic number.; or if a palindromic number cannot be found in 10 iterations, print Not found in 10 iterations. instead.

    Sample Input 1:

    97152
    

    Sample Output 1:

    97152 + 25179 = 122331
    122331 + 133221 = 255552
    255552 is a palindromic number.
    

    Sample Input 2:

    196
    

    Sample Output 2:

    196 + 691 = 887
    887 + 788 = 1675
    1675 + 5761 = 7436
    7436 + 6347 = 13783
    13783 + 38731 = 52514
    52514 + 41525 = 94039
    94039 + 93049 = 187088
    187088 + 880781 = 1067869
    1067869 + 9687601 = 10755470
    10755470 + 07455701 = 18211171
    Not found in 10 iterations.

    Keys:

    • 快乐模拟

    Attention:

    • under algorithm, reverse(s.begin(),s.end());

    Code:

     1 /*
     2 Data: 2019-05-24 20:58:33
     3 Problem: PAT_A1136#A Delayed Palindrome
     4 AC: 25:40
     5 
     6 题目大意:
     7 非回文数转化为回文数;
     8 while(! palindrome){
     9 1.Reverse
    10 2.add
    11 输入:
    12 给一个不超过1000位的正整数
    13 输出:
    14 给出每次循环的加法操作,最多10次循环
    15 */
    16 
    17 #include<cstdio>
    18 #include<string>
    19 #include<iostream>
    20 #include<algorithm>
    21 using namespace std;
    22 
    23 int IsPal(string s)
    24 {
    25     for(int i=0; i<s.size()/2; i++)
    26         if(s[i]!=s[s.size()-1-i])
    27             return 0;
    28     return 1;
    29 }
    30 
    31 string Add(string s1, string s2)
    32 {
    33     string s="";
    34     int num=0;
    35     for(int i=s1.size()-1; i>=0; i--)
    36     {
    37         num += (s1[i]-'0')+(s2[i]-'0');
    38         s += (num%10+'0');
    39         num /= 10;
    40     }
    41     while(num != 0)
    42     {
    43         s += (num%10+'0');
    44         num /= 10;
    45     }
    46     reverse(s.begin(),s.end());
    47     return s;
    48 }
    49 
    50 int main()
    51 {
    52 #ifdef    ONLINE_JUDGE
    53 #else
    54     freopen("Test.txt", "r", stdin);
    55 #endif
    56 
    57     string s,t;
    58     cin >> s;
    59     int pt=0;
    60     while(pt<10)
    61     {
    62         if(IsPal(s))
    63             break;
    64         t=s;
    65         reverse(t.begin(),t.end());
    66         cout << s << " + "<< t << " = ";
    67         s=Add(s,t);
    68         cout << s << endl;
    69         pt++;
    70     }
    71     if(pt<10)
    72         cout << s << " is a palindromic number.";
    73     else
    74         printf("Not found in 10 iterations.");
    75 
    76     return 0;
    77 }

    转载于:https://www.cnblogs.com/blue-lin/p/10920330.html

  • 相关阅读:
    Apache Common DbUtils
    Jackson 框架,轻易转换JSON
    Java数据库ResultSet转json实现
    java获得当前文件路径
    JSP基本面试的试题
    mysql优化方法积累
    linux数据盘分区以及格式化
    linux 数据盘和系统盘的查看
    配置Tomcat使用https协议(配置SSL协议)
    idea-java项目配置
  • 原文地址:https://www.cnblogs.com/twodog/p/12134856.html
Copyright © 2011-2022 走看看