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
題解
思路
建立一個哈希表,存放每個字符出現的次數
將原數乘以2后,和哈希表每個字符出現的次數作對比
完事兒嗷
數據結構
_dict 是一個哈希表,對每個鍵默認值是0
鍵是0到9的字符
值是它出現的次數
算法
對作為字符串的原數,按照每一個字符出現的次數添加到哈希表
將原數乘以2,對每一個出現的字符,哈希表相應減一
如果哈希表不是每一項都為0,那么就說明是No
代碼
因為使用Python能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)
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的python大于小于1023python大于小于_PythonPAT 1023 Have Fun with Numbers的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 桌面消息提醒_对win7的支持已近尾声,
- 下一篇: 已知序列求蝶形运算_(数字信号处理选择题