返回列表 發帖

公因數

  1. raw=input("請依序輸入兩個正整數: ").split(" ")
  2. a=int(raw[0]);
  3. b=int(raw[1]);
  4. smaller=min(a,b)
  5. sum=0
  6. print(a,"與",b,"的公因數有: ",end="")
  7. for i in range(1,smaller+1):
  8.     if a%i==0 and b%i==0:
  9.         print(i,end=" ")
  10.         sum+=1
  11. print("\n共有",sum,"個!")
複製代碼

  1. i = input("請輸入兩個整數並用空格隔開:")
  2. i= i.split()
  3. print(f"{i[0]}和{i[1]}的公因數有:",sep=" ",end="")
  4. a =int(i[0])
  5. b =int(i[1])
  6. sum = 0
  7. small = min(a,b)

  8. for n in range(1, small + 1):
  9.     if (a%n == 0 and b%n == 0):
  10.         print(n,end=", ")
  11.         sum +=1

  12. print(f"共有{sum}個")
複製代碼

TOP

返回列表