首页 > 其他 > 详细

PAT Advanced 1023 Have Fun with Numbers (20分)

时间:2020-01-21 10:12:35      阅读:74      评论:0      收藏:0      [点我收藏+]

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)

PAT Advanced 1023 Have Fun with Numbers (20分)

原文:https://www.cnblogs.com/littlepage/p/12220165.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!