zoukankan      html  css  js  c++  java
  • pat 1136

    1136 A Delayed Palindrome (20分)

     

    Consider a positive integer N written in standard notation with k+1 digits ai?? as a?k???a?1??a?0?? with 0 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 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.

    题意:给定一个数,判断是否为水仙花数,若不是,用这个数和它的逆置数的和取代它继续判断,10次取代之后如果扔不是,结束程序

    思路:algorithm数组下的reverse(s.begin(),s.end())函数可以逆置字符串。

    代码瑞如下:

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<string>
    using namespace std;
    string rev(string s){
        reverse(s.begin(),s.end());
        return s;
    }
    string add(string s1,string s2){
        int count=0;
        int len=s1.length();
        string s=s1;
        for(int i=len-1;i>=0;i--){
            s[i]=(s1[i]-'0'+s2[i]-'0'+count)%10+'0';
            count=(s1[i]-'0'+s2[i]-'0'+count)/10;
        }
        if(count==1)
            s="1"+s;
        return s;
    }
    int main(){
        string s,sum;
        cin>>s;
        if(s==rev(s)){
            cout<<s<<" is a palindromic number." ;
            return 0;
        }
        int mark=0;
        for(int i=0;i<10;i++){
            sum=add(s,rev(s));
            cout<<s<<" + "<<rev(s)<<" = "<<sum<<endl;
            s=sum;
            if(s==rev(s)){
                cout<<s<<" is a palindromic number." ;
                mark=1;
                break;
            }
        }
        if(mark==0){
            cout<<"Not found in 10 iterations.";
        }
        
        return 0;
    }
  • 相关阅读:
    Layer Trees Reflect Different Aspects of the Animation State
    CALayer
    iOS学习笔记09-核心动画CoreAnimation
    CAShapeLayer使用
    iOS动画的要素:CALayer维护数据模型和图片,沟通了CPU和GPU--视图中与图形绘制相关的功能
    CoreAnimation confusion: CATransaction vs CATransition vs CAAnimationGroup?
    圆环,扇形控件基本算法一种实现
    pthread_barrier_init,pthread_barrier_wait简介
    完整详解GCD系列(三)dispatch_group
    GCD学习(五) dispatch_barrier_async
  • 原文地址:https://www.cnblogs.com/foodie-nils/p/13295373.html
Copyright © 2011-2022 走看看