内容 :
各位在國小時都學過因數分解,都瞭解怎麼樣用紙筆計算出結果,現在由你來敎電腦做因數分解。因數分解就是把一個數字,切分為數個質數的乘積,如 12=2^2 * 3
其中, 次方的符號以 ^ 來表示
输入说明 :
一個整數, 大於1 且 小於等於 1000000输出说明 :
一個字串Python:(这个是逗逼,看下面的)
while True :
a = input()
try :
int(a)
except :
print('不是整数哦')
else :
a = int(a)
b = 2
c = []
while a > 1 :
if a % b== 0 :
c.append(b)
a /= b
else :
b += 1
else :
d = {}
for x in c :
if x in d :
d[x] += 1
else :
d[x] = 1
keys = list(d.keys())
keys.sort()
temp=[]
for x in keys :
temp.append(str(x))
if not d[x] == 1 :
temp.append('^'+str(d[x]))
temp.append(' * ')
temp=temp[:-1]
ans=''
print(ans.join(temp))
Python:(这个不逗)
while True :
a = input()
try :
int(a)
except :
print('不是整数哦')
else :
a = int(a)
b = 2
time = 0
ans = ''
while a > 1 :
c = 0
while True :
if a % b== 0 :
c += 1
a /= b
else :
if c==0 :
break
elif c == 1 :
if not time == 0 :
ans = ans + ' * '
ans = ans + str(b)
time += 1
break
else :
if not time == 0 :
ans = ans + ' * '
ans = ans + str(b) + '^' + str(c)
time += 1
break
b += 1
else :
print(ans)