zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    面试中变相考算法复杂度
    黑马程序猿——JAVA面向对象的特性:封装,继承,多态
    Supermap 组合单值专题图与标签专题图演示样例
    线段树 hdu3642 Get The Treasury
    servlet学习(1)
    Androidbutton事件的五中写法总结
    Java多线程的调度策略
    linux命令行学习-dig(DNS查询器)
    kettle(一)概述
    学习C语言,困难吗?
  • 原文地址:https://www.cnblogs.com/sindy/p/4554003.html
Copyright © 2011-2022 走看看