pat Clase A de alta precisión

A1136

#include<iostream>
#include<vector>
using namespace std;

string a;

bool check(vector<int> A)
{
    
    
    for(int i=0,j=A.size()-1; i<j; i++,j--)
        if(A[i]!=A[j])
            return false;
    return true;
}

void print(vector<int> A)
{
    
    
    for(int i=0; i<A.size(); i++)
        cout << A[i];
}

vector<int> add(vector<int> A, vector<int> B)    //高精度加法计算
{
    
    
    vector<int> C;
    for(int i=0,t=0; i<A.size() || i<B.size() || t; i++)
    {
    
    
        if(i<A.size())
        {
    
    
            t+=A[i];
        }

        if(i<B.size())
        {
    
    
            t+=B[i];
        }

        C.push_back(t%10);
        t/=10;
    }
    vector<int> DD(C.rbegin(),C.rend());
    return DD;
}

int main()
{
    
    
    cin >> a;      //输入字符串a
    vector<int> A;

    for(int i=0; i<a.size(); i++)   //将字符串a放到vector A中
    {
    
    
        A.push_back(a[i]-'0');
    }

    for(int i=0; i<10; i++)      //一共遍历 循环 10次
    {
    
    
        if(check(A))    //如果A是回文直接退出循环
            break;
        vector<int> B(A.rbegin(),A.rend());   //B就是A倒着的 rbegin()指向最后一个元素,rend()指向第一个元素,

        //进行输出
        print(A);
        cout << " + ";
        print(B);
        cout << " = ";

        A=add(A,B);

        print(A);

        cout << endl;
    }

    if(check(A))    //现在的A是经过了10次计算,然后再判断下是否是回文
    {
    
    
        print(A);
        cout << " is a palindromic number.";
    }
    else
    {
    
    
        cout << "Not found in 10 iterations.";
    }

}

Supongo que te gusta

Origin blog.csdn.net/wsfhdhjs/article/details/110440021
Recomendado
Clasificación