00-自测4. 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 file 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<iostream>
#include<string>
using namespace std;
int main() {
    int i = 0;
    int j = 0;
    int len;
    int temp;
    string num;
    //判断是否为输入的副本
    bool is_dup = true;
    int judge[20];
    //保存输出结果
    int result[21];
    //保存进位
    int counter[20];

    cin >> num;
    len = num.length();

    for (i = 0; i < 20; i++) {
        counter[i] = 0;
        judge[i] = 0;
    }
    //计算输出结果
    for (i = len - 1; i >= 0; i--) {
        temp = (num[i] - '0');
        judge[temp]++;
        temp = temp * 2 + counter[i];
        result[j++] = temp % 10;
        if (i != 0)
            counter[i - 1] = temp / 10;
        else {
            result[j++] = temp / 10;
        }
    }
    if (result[j - 1] != 0)
        len = j;

    for (i = 0; i < len; i++) {
        judge[result[i]]--;
    }

    for (i = 0; i < 10; i++) {
        if (judge[i] != 0) {
            is_dup = false;
        }
    }

    if (is_dup)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;

    for (j = len - 1; j >= 0; j--) {
        cout << result[j];
    }
    return 0;
}

 

posted @ 2015-06-05 10:34  Sindyang  阅读(410)  评论(0编辑  收藏  举报