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

版权声明:首发于 www.amoshuang.com https://blog.csdn.net/qq_35499060/article/details/82086012

用Python写PAT甲级,答案都在这儿

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

题目大意

给定一个大整数,如果这个大整数的两倍每个数字出现的次数和原本的数字中每个数字出现的次数相同,则输出Yes,否则输出No。最后输出大整数乘以两倍的结果。

分析

直接乘以两倍,使用collections.Counter来统计各个数字的出现次数

Python实现

from collections import Counter
    
if __name__ == "__main__":
    num = input()
    n = str(int(num)*2)
    a = Counter(num)
    b = Counter(n)
    if a==b:
        print("Yes")
    else:
        print("No")
    print(n)

猜你喜欢

转载自blog.csdn.net/qq_35499060/article/details/82086012