[PAT brush questions] AcWing 1500. Interesting numbers (high precision addition)

Thought

Common data ranges in C++:
int: -2 * 10e9 ~ 2 * 10e9
long long: -9 * 10e18 ~ 9 * 10e18

High-precision addition core code

for (int i = 0; i < a.size(); i++) 
{
    
    
     int s = a[i] + a[i] + t;
     b.push_back(s % 10);
     t = s / 10;
}

topic

Note that the number 123456789 is a 9-digit number, exactly from 1 to 9

composition, without repetition.

Doubling that, we get 246913578
, which happens to be another 9-digit number, exactly from 1 to 9

The composition, just the arrangement is different.

Now, given you a k

digit number, please judge whether the number obtained after doubling it can be obtained by rearranging the digits of the original number.
input format

One line containing an integer.
output format

The output is two lines

If the digits of the original number are rearranged to obtain the doubled number, output Yes in the first line, otherwise output No.

On the second line, output the number obtained after doubling.
data range

Enter a number up to 20

bit.
Input sample:

1234567899

Sample output:

Yes
2469135798

c++ code

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;
typedef long long LL;

int main()
{
    
    
    
    string A;
    vector<int> a;
    
    cin >> A;
    for (int i = A.size() - 1; i >= 0; i--)
        a.push_back(A[i] - '0');
    
    vector<int> b;
    int t = 0;
    for (int i = 0; i < a.size(); i++) 
    {
    
    
        int s = a[i] + a[i] + t;
        b.push_back(s % 10);
        t = s / 10;
    }
    
    if (t) b.push_back(t);
    vector<int> c = b;
    sort(a.begin(), a.end());
    sort(c.begin(), c.end());
    
    if (a == c) puts("Yes");
    else puts("No");
    
    for (int i = b.size() - 1; i >= 0; i--) cout << b[i];
    
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326128110&siteId=291194637