问大家一个PYTHON问题

m
majia666
楼主 (北美华人网)
这段程序第六行红色德那句, 怎么会返回三个数?
# function for extended Euclidean Algorithm     def gcdExtended(a, b):       # Base Case     if a == 0:         return b, 0, 1       gcd, x1, y1 = gcdExtended(b % a, a)       # Update x and y using results of recursive     # call     x = y1 - (b//a) * x1     y = x1       return gcd, x, y
g
gokgs
最后一行返回三个参数阿。 哈哈。 so easy. 就是个 tuple, 几个都行, 尽管我看着头都大。
f
felali9
o
oneleaf
python 是可以返回多个参数的
m
majia666
回复 2楼gokgs的帖子
那个GCD 的值是从哪里算出来的?
h
hify
这个function返回三个数啊 return那儿
g
gokgs
回复 2楼gokgs的帖子
那个GCD 的值是从哪里算出来的?
majia666 发表于 2022-12-21 21:10

递归调用阿, base case 是 第一个 return , a == 0.
这种带 共产党 (gcd) 的 面试一般不会考, 哈哈。
好像有个 library function 可以 call.
m
majia666
递归调用阿, base case 是 第一个 return , a == 0.
这种带 共产党 (gcd) 的 面试一般不会考, 哈哈。
好像有个 library function 可以 call.
gokgs 发表于 2022-12-21 21:14

高中小孩作业。我不会PYTHON, 但基本概念还是有。
a
aiyamayayongle
等号左边有三个参数,那可不就是返回三个值么。
星月花草
回复 1楼majia666的帖子
这个function不就返回了三个值吗?你这里不就是调用了这个function?
大喜妞
你的return 返回的就是三个数值。 这个算是最基本的python概念了。
g
gokgs
高中小孩作业。我不会PYTHON, 但基本概念还是有。
majia666 发表于 2022-12-21 21:33

我还以为你在刷题呢, 哈哈。 连三个返回值都看不出来, 就不要吹了。 LOL
千渔千寻
回复 5楼majia666的帖子
辗转相除法,递归算出来的。
C
Cumberbitch
高中小孩作业。我不会PYTHON, 但基本概念还是有。
majia666 发表于 2022-12-21 21:33

基本概念你就没有
你别辅导了,你远不如你小孩
e
emarald
等号左边有三个参数,那可不就是返回三个值么。
aiyamayayongle 发表于 2022-12-21 21:40

我猜楼主就是看等号右边了。。。
s
simonxp
Answer from chatgpt This is an implementation of the extended Euclidean algorithm, which is an algorithm that takes in two positive integers a and b and returns the greatest common divisor of a and b, as well as the coefficients x and y such that ax + by = gcd(a, b). The algorithm works by recursively applying the Euclidean algorithm to find the greatest common divisor of a and b, but also keeps track of the coefficients x and y as it goes. The base case is when a is equal to 0, in which case the greatest common divisor is b and the coefficients x and y are 0 and 1, respectively. Otherwise, the algorithm recursively computes the greatest common divisor of b % a and a, as well as the coefficients x1 and y1. It then uses these values to compute the coefficients x and y using the equation x = y1 - (b//a) * x1 and y = x1. Finally, it returns the greatest common divisor, x, and y.
私用牛甲1999
高中就学python了
x
xiaxie8
这段程序第六行红色德那句, 怎么会返回三个数?
# function for extended Euclidean Algorithm     def gcdExtended(a, b):       # Base Case     if a == 0:         return b, 0, 1       gcd, x1, y1 = gcdExtended(b % a, a)       # Update x and y using results of recursive     # call     x = y1 - (b//a) * x1     y = x1       return gcd, x, y
majia666 发表于 2022-12-21 20:10

看了您的问题,我感觉您只接触过C/C++/Java,肯定不懂Fortran。
t
tyjz
recursive自己调用自己,返回值就是三个数呀
L
LEAUPARKENZO
高中就学recursion了?