python大于小于1023python大于小于_PythonPAT 1023 Have Fun with Numbers
題目
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:
11234567899
Sample Output:
1
2Yes
2469135798
題解
思路
建立一個(gè)哈希表,存放每個(gè)字符出現(xiàn)的次數(shù)
將原數(shù)乘以2后,和哈希表每個(gè)字符出現(xiàn)的次數(shù)作對(duì)比
完事兒嗷
數(shù)據(jù)結(jié)構(gòu)
_dict 是一個(gè)哈希表,對(duì)每個(gè)鍵默認(rèn)值是0
鍵是0到9的字符
值是它出現(xiàn)的次數(shù)
算法
對(duì)作為字符串的原數(shù),按照每一個(gè)字符出現(xiàn)的次數(shù)添加到哈希表
將原數(shù)乘以2,對(duì)每一個(gè)出現(xiàn)的字符,哈希表相應(yīng)減一
如果哈希表不是每一項(xiàng)都為0,那么就說(shuō)明是No
代碼
因?yàn)槭褂肞ython能AC,因此只放了Python的代碼。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17from collections import defaultdict
_dict = defaultdict(int)
a = input()
for i in a:
_dict[i] += 1
a = str(int(a) * 2)
for i in a:
_dict[i] -= 1
for i in "0123456789":
if _dict[i] != 0:
print("No")
break
else:
print("Yes")
print(a)
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的python大于小于1023python大于小于_PythonPAT 1023 Have Fun with Numbers的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 桌面消息提醒_对win7的支持已近尾声,
- 下一篇: python从多层循环嵌套中退出_pyt