自測(cè)-4 Have Fun with Numbers(20 分)

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

import java.math.BigInteger;
import java.util.Scanner;


public class Main{
    public static boolean judge(String nums,String doubled){
        char []exaclty = new char[10];
        if (nums.length() != doubled.length()) {
            return false;
        }
        for (int i = 0; i < nums.length(); i++) {
            int num = nums.charAt(i) - '0';
            exaclty[num]++;
        }
        for (int i = 0; i < doubled.length(); i++) {
            int num = doubled.charAt(i)-'0';
            exaclty[num]--;
        }
        for (int i = 0; i < exaclty.length; i++) {
            if (exaclty[i] != 0) {
                return false;
            }
        }
        return true;
    }
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String nums = in.next();
        BigInteger doubling = new BigInteger(nums);
        doubling = doubling.shiftLeft(1);
        String doubled = String.valueOf(doubling);
        if (judge(nums,doubled)) {
            System.out.println("Yes");
        }else{
            System.out.println("No");
        }
        System.out.println(doubling);
    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗(yàn)。 張土汪:刷leetcod...
    土汪閱讀 12,923評(píng)論 0 33
  • 他每天上班都會(huì)去那家包子鋪買包子,旁邊總是坐著一位雙目失明的老乞丐,前面放著個(gè)鐵盒子。他總會(huì)扔給他一元錢,可是他從...
    阿正s閱讀 225評(píng)論 0 2
  • 使用了MJExtension
    Paco_Ke閱讀 1,172評(píng)論 0 2
  • 親愛的媽媽: 您好! 轉(zhuǎn)眼間,我已成為了名副其實(shí)的大學(xué)生。驟然此時(shí)才發(fā)現(xiàn),時(shí)間竟是如此的調(diào)皮,只在剎那之間溜得這...
    蒽得閱讀 333評(píng)論 0 0

友情鏈接更多精彩內(nèi)容