PAT 1023 Have Fun with Numbers python解法

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

题意:给你一个数,如果它加倍之后的数中数字和原来的数一样,就输出Yes,否则输出No,(比如说123,加倍之后是246,数字完全不一样,应该输出No,再比如234567891,加倍后是469135782,都有数字1-9,应该输出Yes),并在下一行输出该数加倍后的结果。

解题思路:根据题意,有一个很简单的想法是:将输入的数s中每一位放入一个列表l1并排序,再把s加倍后的d也放入一个列表l2并排序,这样操作之后,只需要判断l1和l2是否相等就可以了,相等则输出Yes,否则输出No,然后输出d。

s = input()
l1 = [i for i in s]
l1.sort()
d = str(int(s)*2)
l2 = [i for i in d]
l2.sort()
if l1 == l2:
    print('Yes')
    print(d)
else:
    print('No')
    print(d)

猜你喜欢

转载自blog.csdn.net/qq_36936510/article/details/86376379