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)
  • 相关阅读:
    JavaScript 深入了解对象中的属性
    JavaScript 开发规范
    vue 项目接口管理
    放大镜特效
    多用户ATM机(面向对象编程)
    浏览器检查块代码
    js中innerHTML与innerText的用法与区别
    symbol访问法及symbor注册表
    最常用的15个前端表单验证JS正则表达式
    数组的遍历
  • 原文地址:https://www.cnblogs.com/littlepage/p/12220165.html
Copyright © 2011-2022 走看看