zoukankan      html  css  js  c++  java
  • PAT Advanced 1023 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

    这道题考察了大数处理,遇到大数,首选Java或者Python

    Java

    import java.math.BigInteger;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            boolean right = true;
            BigInteger b1, b2;
            b1 = sc.nextBigInteger();
            b2 = b1.multiply(new BigInteger("2"));
            int[] a = new int[10];
            int[] b = new int[10];
            String s1 = b1.toString();
            String s2 = b2.toString();
            for(int i=0;i<s1.length();i++)
                a[s1.charAt(i)-'0']++;
            for(int i=0;i<s2.length();i++)
                b[s2.charAt(i)-'0']++;
            for(int i=0;i<10;i++)
                if(a[i]!=b[i]) right = false;
            if(right) System.out.println("Yes");
            else System.out.println("No");
            System.out.println(s2);
        }
    }

    Python

    a = int(input())
    b = a*2
    arr = []
    arr2 = []
    for i in range(10):
        arr.append(0)
        arr2.append(0)
        
    for x in str(a):
        index = int(x)
        arr[index] = arr[index]+1
    for x in str(b):
        index = int(x)
        arr2[index] = arr2[index]+1
    
    right = True
    for i in range(10):
        if(arr[i] != arr2[i]):
            right = False
    
    if(right):
        print("Yes")
    else:
        print("No")
    
    print(b)
  • 相关阅读:
    MySQL SQL语句总结
    JavaFx通用查询、跳转创建、修改页面、关闭舞台工具类
    反射工具类
    SpringBoot将数据写入word模板,生成word文档
    vscode设置中文的方法
    Flex 布局
    处理程序“ExtensionlessUrlHandler-Integrated-4.0”在其模块列表中有一个错误模块“ManagedPipelineHandler” •使用了托管的处理程序,但是未安装或未完整安装 ASP.NET。
    第一周助教总结
    JS时间转换
    ES 安装常见错误
  • 原文地址:https://www.cnblogs.com/littlepage/p/12220165.html
Copyright © 2011-2022 走看看