PAT 1023 Have Fun with Numbers (大数相乘)

Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a different permutation. Check to see the result if we double it again!

Now you are suppose to check if there are more numbers with this property. That is, double a given number with k digits, you are to tell if the resulting number consists of only a permutation of the digits in the original number.

Input Specification:

Each input contains one test case. Each case contains one positive integer with no more than 20 digits.

Output Specification:

For each test case, first print in a line "Yes" if doubling the input number gives a number that consists of only a permutation of the digits in the original number, or "No" if not. Then in the next line, print the doubled number.

Sample Input:

1234567899

Sample Output:

Yes
2469135798

思路

大数相乘

代码

#include <stdio.h>
#include <string>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <map>
#include <queue>
#include <stack>
#include <functional>
#include <limits.h> 
using namespace std;


int main() {
    string a;
    cin >> a;
    reverse(a.begin(), a.end());
    int num[100];   memset(num, 0, sizeof(num));
    string ans = "";
    int carry = 0;
    int flag = true;
    for(int i = 0; i < a.length(); i++){
        num[a[i] - '0']++;
        ans.push_back((2 * (a[i] - '0') + carry) % 10  + '0');
        carry = (2 * (a[i] - '0') + carry) / 10;
    }
    if(carry){
        ans.push_back(carry + '0');
    }
    for(int i = 0; i < ans.length(); i++){
        num[ans[i] - '0']--;
    }
    for(int i = 0; i < 15; i++){
        if(num[i] != 0){
            flag = false;
            break;
        }
    }
    reverse(ans.begin(), ans.end());
    if(flag)    cout << "Yes" << endl;
    else cout << "No" << endl;
    cout << ans << endl;
    return 0; 
}

猜你喜欢

转载自www.cnblogs.com/woxiaosade/p/12404733.html