日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python程序:計算兩個數(shù)的GCD

創(chuàng)新互聯(lián)Python教程:

目前創(chuàng)新互聯(lián)建站已為上千家的企業(yè)提供了網(wǎng)站建設、域名、網(wǎng)站空間、網(wǎng)站改版維護、企業(yè)網(wǎng)站設計、撫寧網(wǎng)站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

編寫一個 Python 程序,使用 While 循環(huán)、函數(shù)和遞歸來查找兩個數(shù)的 GCD。要在 Python 中找到 GCD 或 HCF,我們必須至少傳遞一個非零值

最大公約數(shù)也被稱為最高公因數(shù),或最大公約數(shù),或最高公約數(shù)(HCD),或最大公約數(shù)。

在數(shù)學中,兩個或兩個以上整數(shù)的最大公約數(shù)(GCD)是給定整數(shù)值除以沒有余數(shù)的最大正整數(shù)。例如,整數(shù) 8 和 12 的 GCD 值為 4,因為 8 和 12 都可以被 1、2 和 4 整除(余數(shù)為 0),其中最大的正整數(shù)為 4。

Python 程序查找兩個數(shù)的 GCD 示例 1

這個 python 程序允許用戶輸入兩個正整數(shù)值。接下來,我們使用 Python While 循環(huán)來限制 I 值不超過用戶指定的值。

在 Python While 循環(huán)中,我們使用 If 語句來檢查%i 和% i 余數(shù)是否等于零。如果為真,則最高公因數(shù)= 1,否則跳過該值。

# Python Program to find GCD of Two Numbers

a = float(input(" Please Enter the First Value a: "))
b = float(input(" Please Enter the Second Value b: "))

i = 1
while(i <= a and i <= b):
    if(a % i == 0 and b % i == 0):
        val = i
    i = i + 1

print("\n HCF of {0} and {1} = {2}".format(a, b, val))
 Please Enter the First Value a: 8
 Please Enter the Second Value b: 12

 HCF of 8.0 and 12.0 = 4

Python 程序查找兩個數(shù)的 HCF 示例 2

這是另一種求兩個數(shù)最大公因數(shù)的方法。在這個 python 程序中,我們使用 Temp 變量來查找 GCD。

num1 = float(input(" Please Enter the First : "))
num2 = float(input(" Please Enter the Second : "))

a = num1
b = num2

while(num2 != 0):
    temp = num2
    num2 = num1 % num2
    num1 = temp

hcf = num1   
print("\n HCF of {0} and {1} = {2}".format(a, b, hcf))
 Please Enter the First : 12
 Please Enter the Second : 36

 HCF of 12.0 and 36.0 = 12.0

Python 程序不用 Temp 就能找到兩個數(shù)的 GCD

在這個程序中,我們在不使用 Temp 變量的情況下找到了 GCD。

num1 = float(input(" Please Enter the First : "))
num2 = float(input(" Please Enter the Second : "))

a = num1
b = num2

if(num1 == 0):
    print("\n HCF of {0} and {1} = {2}".format(a, b, b))

while(num2 != 0):
    if(num1 > num2):
        num1 = num1 - num2
    else:
        num2 = num2 - num1

hcf = num1   
print("\n HCF of {0} and {1} = {2}".format(a, b, hcf))
 Please Enter the First : 75
 Please Enter the Second : 255

 HCF of 75.0 and 255.0 = 15.0

用函數(shù)求兩個數(shù)的 GCD 的 Python 程序

這個 Python 程序同上。然而,我們使用功能 來分離邏輯

def findgcd(num1, num2):
    if(num1 == 0):
        print("\n HCF of {0} and {1} = {2}".format(a, b, b))

    while(num2 != 0):
        if(num1 > num2):
            num1 = num1 - num2
        else:
            num2 = num2 - num1
    return num1

a = float(input(" Please Enter the First Value a: "))
b = float(input(" Please Enter the Second Value b: "))

gcd = findgcd(a, b)  
print("\n HCF of {0} and {1} = {2}".format(a, b, gcd))

用遞歸法計算兩個數(shù)的 GCD 的 Python 程序

它允許用戶輸入兩個正整數(shù)值,并通過遞歸調(diào)用 findGreatestCD 函數(shù)來計算這兩個值的最大公約數(shù)。

def findGreatestCD(a, b):
    if(b == 0):
        return a;
    else:
        return findGreatestCD(b, a % b)

num1 = float(input(" Please Enter the First Value  Num1 : "))
num2 = float(input(" Please Enter the Second Value Num2 : "))

Val = findGreatestCD(num1, num2)
print("\n GCD of {0} and {1} = {2}".format(num1, num2, Val))
 Please Enter the First Value  Num1 : 22
 Please Enter the Second Value Num2 : 88

 GCD of 22.0 and 88.0 = 22.0

本文標題:Python程序:計算兩個數(shù)的GCD
文章源于:http://www.5511xx.com/article/ccsddgo.html