• [PAT] 1023 Have Fun with Numbers (20 分)Java


    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


     1 import java.math.BigInteger;
     4 import java.util.ArrayList;
     5 import java.util.Collections;
     6 import java.util.Scanner;
     7 
     8 /**
     9  * @Auther: Xingzheng Wang
    10  * @Date: 2019/2/21 18:07
    11  * @Description: pattest
    12  * @Version: 1.0
    13  */
    14 public class PAT1023 {
    15     public static void main(String[] args) {
    16         Scanner sc = new Scanner(System.in);
    17         BigInteger number = sc.nextBigInteger();
    18         BigInteger result = number.multiply(new BigInteger("2"));
    19         ArrayList<Character> list1 = new ArrayList();
    20         ArrayList<Character> list2 = new ArrayList();
    21         sortOfNumber(list1,number);
    22         sortOfNumber(list2,result);
    23 
    24         if(list1.equals(list2))
    25             System.out.println("Yes");
    26         else
    27             System.out.println("No");
    28         System.out.println(result);
    29     }
    30     private static void sortOfNumber(ArrayList<Character> list,BigInteger number){
    31         String s = number.toString();
    32         char[] ch = s.toCharArray();
    33         for (char c : ch){
    34             list.add(c);
    35         }
    36         Collections.sort(list);
    37     }
    38 }
  • 相关阅读:
    移动应用专项测试
    MAC连接安卓手机通过adb指令安装apk
    Git GUI可视化操作教程
    nestjs中typeorm进行事物操作
    vue-element-admin 实现动态路由(从后台查询出菜单列表绑定侧边栏)
    el-form 表单校验
    vscode设置VUE eslint开发环境
    .netcore signalR 实时消息推送
    psexec局域网执行远程命令
    Asp.Net跨平台 Jexus 5.8.1 独立版
  • 原文地址:https://www.cnblogs.com/PureJava/p/10498039.html
Copyright © 2020-2023  润新知