zoukankan      html  css  js  c++  java
  • 1136 A Delayed Palindrome (20)

    Consider a positive integer N written in standard notation with k+1 digits a~i~ as a~k~...a~1~a~0~ with 0 <= a~i~ < 10 for all i and a~k~ > 0. Then N is palindromic if and only if a~i~ = a~k-i~ 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 fromhttps://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.
    
     输入数的长度最长有1000位,应该用string来保存输入数据
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<string>
     4 using namespace std;
     5 string add(string a, string b){
     6   int l=a.size(), carry=0;
     7   for(int i=l-1; i>=0; i--){
     8     a[i]=b[i]+a[i]+carry-'0';
     9     if(a[i]>'9'){
    10       carry=1;
    11       a[i] -= 10;
    12     }else carry=0;
    13   }
    14   if(carry==1) a = '1'+a;
    15   return a;
    16 }
    17 int main(){
    18   int i;
    19   bool flag=true;
    20   string n, num, copy;
    21   cin>>n;
    22   for(i=0; i<10; i++){
    23     copy=num=n;
    24     reverse(num.begin(), num.end());
    25     if(num==n){ cout<<num<<" is a palindromic number."<<endl; flag=false; break;}
    26     n=add(n, num);
    27     cout<<copy<<" + "<<num<<" = "<<add(copy, num)<<endl;
    28   }
    29   if(flag) cout<<"Not found in 10 iterations."<<endl;
    30   return 0;
    31 }
    有疑惑或者更好的解决方法的朋友,可以联系我,大家一起探讨。qq:1546431565
  • 相关阅读:
    很多的技术招聘面试方式不务实-导致不仅难以招聘到人,而且严重损害公司形象
    技术工作者上升到思想,哲学层面也许更好
    程序员与架构师的区别
    (转载)创业型公司如何管理-吸引人才
    C#图片转成流,流转成图片,字节转图片,图片转字节的方法
    C# Linq获取两个List或数组的差集交集
    C# List排序,附加使用Linq排序
    C#Qrcode生成二维码支持中文,带图片,带文字
    C#判断本地文件,网络文件是否存在是否存在
    C#WebBrowser控件使用教程与技巧收集
  • 原文地址:https://www.cnblogs.com/mr-stn/p/9161612.html
Copyright © 2011-2022 走看看