我的第三次解答:
num = int(input())
k = dict()
while num != 1:
for i in range(2,num+1):
if num%i == 0:
if i in k.keys():
k[i] = k[i] + 1
else:
k[i] = 1
num = num//i
break
Str = ''
for i in k.keys():
if k[i] != 1:
Str = Str+' * '+str(i)+'^'+str(k[i])
else:
Str = Str+' * '+str(i)
print(Str.lstrip(' * '))
我的第二次解答:
factors = []
PrimeSet = set()
CompositeSet = set()
Str = ''
InputNumber = int(input())
# 因數集合
for i in range(2,InputNumber+1):
if InputNumber%i == 0:
factors.append(i)
for j in range(2,i):
if i%j == 0:
CompositeSet.add(i)
PrimeSet = set(factors) - CompositeSet
for i in sorted(PrimeSet):
k = 0
while InputNumber%i == 0:
k = k + 1
InputNumber = InputNumber//i
if k == 1:
Str = Str+' * '+str(i)
else:
Str = Str+' * '+str(i)+'^'+str(k)
print(Str.lstrip(' * '))
檢測結果:
factors = []
PrimeSet = set()
CompositeSet = set()
InputNumber = int(input())
Power = dict()
# 因數集合
for i in range(1,InputNumber+1):
if InputNumber%i == 0:
factors.append(i)
# 找因數集合內的合數集合
for i in factors:
if i > 1:
for j in range(2,i):
if i%j == 0:
CompositeSet.add(i)
# 質數集合
PrimeSet = set(factors) - {1} - CompositeSet
# 質數集合,找各質數的次方
for i in sorted(PrimeSet):
quotient = InputNumber//i
k = 1
while quotient%i == 0:
k=k+1
quotient = quotient//i
Power[i] = k
# 調整輸出
Str=''
for i in Power.keys():
j = Power[i]
if j == 1:
Str = Str+' * '+str(i)
else:
Str = Str+' * '+str(i)+'^'+str(j)
# 輸出
print(Str.lstrip(' * '))
檢測結果:
資料來源:
1.高中生解題系統
1.高中生解題系統
ZeroJudge解題
ZeroJudge python解答:a004. 文文的求婚 ZeroJudge python解答:a005. Eva 的回家作業
ZeroJudge python解答:a006. 一元二次方程式
ZeroJudge python解答:a009. 解碼器
ZeroJudge python解答:a010. 因數分解
ZeroJudge python解答:a015. 矩陣的翻轉
ZeroJudge python解答:a017. 五則運算
ZeroJudge python解答:a020. 身分證檢驗
ZeroJudge python解答:a021. 大數運算
ZeroJudge python解答:a022. 迴文
ZeroJudge python解答:a024. 最大公因數(GCD)
ZeroJudge python解答:a034. 二進位制轉換
ZeroJudge python解答:a038. 數字翻轉
ZeroJudge python解答:a040. 阿姆斯壯數
ZeroJudge python解答:a042. 平面圓形切割
ZeroJudge python解答:a044. 空間切割
ZeroJudge python解答:a053. Sagit's 計分程式
ZeroJudge python解答:a054. 電話客服中心
ZeroJudge python解答:a058. MOD3
ZeroJudge python解答:a059. 完全平方
ZeroJudge python解答:a065. 提款卡密碼
ZeroJudge python解答:a104. 排序
ZeroJudge python解答:a121. 質數又來囉
ZeroJudge python解答:a148. You Cannot Pass?!
ZeroJudge python解答:a149. 乘乘樂
ZeroJudge python解答:a215. 明明愛數數
ZeroJudge python解答:a216. 數數愛明明
ZeroJudge python解答:a224. 明明愛明明
ZeroJudge python解答:a225. 明明愛排列
ZeroJudge python解答:a244. 新手訓練 ~ for + if
ZeroJudge python解答:a248. 新手訓練 ~ 陣列應用
ZeroJudge python解答:a263. 日期差幾天
ZeroJudge python解答:a410. 解方程
ZeroJudge python解答:a528. 大數排序
ZeroJudge python解答:a647. 投資專家
ZeroJudge python解答:a693. 吞食天地
ZeroJudge python解答:a738. 最大公约数
沒有留言:
張貼留言