Algorithm Training palindrome Number

Source: Internet
Author: User
Tags time limit

Algorithm Training palindrome Number
Time limit: 1.0s memory limit: 256.0MB
Submit this Title 1 Jin Sac 2
Problem description
If a number (first not 0) is read from left to right and read from right to left, we call it a palindrome number.
For example: Given a 10 binary number 56, will 56 plus 65 (that is, 56 right-to-left reading), to get 121 is a palindrome number.

Another example: for 10 binary number 87:
step1:87+78 = 165 step2:165+561 = 726
step3:726+627 = 1353 step4:1353+3531 = 4884

One step here is to make an n-ary addition, and the above example uses at least 4 steps to get the palindrome number 4884.

Write a program, given an N (2<=n<=10 or n=16) binary number m (where 16 binary number is 0-9 and a-f), the minimum number of steps can be obtained palindrome numbers.
If the palindrome number cannot be obtained within 30 steps (including 30 steps), the output "impossible!"
Input format
Two lines, N and M
Output format
If you can get palindrome number within 30 steps, Output "step=xx" (without quotation marks), where xx is the number of steps; otherwise output line "impossible!" (without quotation marks)
Sample input
9
87
Sample output
Step=6

http://lx.lanqiao.cn/problem.page?gpid=T74

The law of the addition of the additive adds precision traversal. Flood problem

#include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> using namespace std;
int d[10001];
    int run (int n) {int z=0;
    int l=10000;
    int p[10001];
    memset (P,0,sizeof (p));

    while (d[l]==0) l--;
        for (int i=0;i<=l;i++) {p[i]=d[l-i];
    cout<<d[l-i];


    }//cout<<endl<<endl;
        for (int i=0;i<10000;i++) {d[i]=d[i]+p[i]+z;
        z=d[i]/n;
    D[i]%=n;
    } l=10000;
    while (d[l]==0) l--;
    /* for (int j=l;j>=0;j--) {cout<<d[j];
    } cout<<endl<<endl;*/for (int j=0,i=l;i>=0;i--, J + +) {if (D[i]!=d[j]) return 0;
} return 1;
    } int main () {int n;
        while (cin>>n) {memset (d,0, sizeof (d));
        string S;
        cin>>s;
        int i;
            for (i=0;s[i];i++) {if (s[i]>= ' 0 ' &&s[i]<= ' 9 ') d[i]=s[i]-' 0 '; else d[i]=s[i]-' A ' +10; } for (i=1;i<30;i++) if (run (n)) {cout<< "step=" <<i<<e
                Ndl
            Break } if (i==30) cout<< "impossible!"
    <<endl;
} return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.