• 1024 Palindromic Number(大数加法)


    1024 Palindromic Number (25 分)

    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:

    484
    2
    

    Sample Input 2:

    69 3
    

    Sample Output 2:

    1353
    3

    思路:
      1、使用字符串存储整数,然而我没有直接使用字符串进行运算,而是使用的vector数组,写的有点烦琐
      2、注意有可能不需要进行操作就是回文串,这样输出的步数应该是0
    #include<iostream>
    #include<vector>
    #include<string>
    #include<algorithm>
    
    using namespace std;
    vector<int> v1,v2,sum;
    
    void add(int n)
    {
        int temp=0;//进位
        for(int i=0;i<n;i++)
        {
            int a=v1[i]+v2[i]+temp;
            //cout<<v1[i]<<" "<<v2[i]<<endl;
            if(a>=10)
            {
                a-=10;
                temp=1;
            }
            else
                temp=0;
            sum.push_back(a);
        }
        if(temp>0)
            sum.push_back(temp);
        reverse(sum.begin(),sum.end());
    }
    
    bool isPad()
    {
        for(int i=0,j=sum.size()-1;i<j;i++,j--)
            if(sum[i]!=sum[j])
                return false;
        return true;
    }
    
    void output(int k)
    {
        for(auto num:sum)
            cout<<num;
        cout<<endl;
        cout<<k<<endl;
    }
    
    void computer(int &k)
    {
        int n=v1.size();
        int i=0;
        while(k>0)
        {
            i++;
            sum.clear();
            add(n);
           // output(i);
            if(isPad())
            {
                output(i);
                return;
            }
            n=sum.size();
            v1.clear();
            v2.clear();
            v1.resize(n);
            v2.resize(n);
            copy(sum.begin(),sum.end(),v1.begin());
            copy(sum.begin(),sum.end(),v2.begin());
            reverse(v2.begin(),v2.end());
            k--;
        }
        output(i);
    }
    
    int main()
    {
        string num;
        int k;
        cin>>num>>k;
        int n=num.size();
        v1.resize(n);
        for(int i=0;i<n;i++)
        {
            v1[i]=num[i]-'0';
           // cout<<v1[i]<<endl;
        }
        v2.resize(n);
        sum.resize(n);
        copy(v1.begin(),v1.end(),v2.begin());
        copy(v1.begin(),v1.end(),sum.begin());
        reverse(v2.begin(),v2.end());
        if(isPad())
        {
            output(0);
        }
        else
        {
            computer(k);
        }
        return 0;
    }


  • 相关阅读:
    软件构造 第三章第一节 数据类型与类型检查
    类图总结
    【Beta】Scrum07
    【Beta】用户问题反馈及处理(一直更新)
    【Beta】第七次任务发布
    【Beta】Scrum06
    【Beta】第六次任务发布
    【Beta】Scrum5.5
    【Beta】第5.5次任务发布
    【Beta】Scrum05
  • 原文地址:https://www.cnblogs.com/zhanghaijie/p/10295045.html
Copyright © 2020-2023  润新知