运行 ❯
获取你
自己的
网站
×
更改方向
更改主题,深色/浅色
前往 Spaces
Python
C
Java
def gcd_subtraction(a, b): while a != b: if a > b: print(f"{a} - {b} = {a-b}") a = a - b else: print(f"{b} - {a} = {b-a}") b = b - a return a a = 120 b = 25 print("The Euclidean algorithm using subtraction:\n") print(f"The GCD of {a} and {b} is: {gcd_subtraction(a, b)}") #Python
#include <stdio.h> int gcdSubtraction(int a, int b) { while (a != b) { if (a > b) { printf("%d - %d = %d\n", a, b, a - b); a = a - b; } else { printf("%d - %d = %d\n", b, a, b - a); b = b - a; } } return a; } int main() { int a = 120; int b = 25; printf("The Euclidean algorithm using subtraction:\n\n"); printf("The GCD of %d and %d is: %d\n", a, b, gcdSubtraction(a, b)); return 0; } //C
public class Main { public static int gcdSubtraction(int a, int b) { while (a != b) { if (a > b) { System.out.println(a + " - " + b + " = " + (a - b)); a = a - b; } else { System.out.println(b + " - " + a + " = " + (b - a)); b = b - a; } } return a; } public static void main(String[] args) { int a = 120; int b = 25; System.out.println("The Euclidean algorithm using subtraction:\n"); System.out.println("The GCD of " + a + " and " + b + " is: " + gcdSubtraction(a, b)); } } //Java
Python 结果
C 结果
Java 结果
使用减法的欧几里得算法
120 - 25 = 95
95 - 25 = 70
70 - 25 = 45
45 - 25 = 20
25 - 20 = 5
20 - 5 = 15
15 - 5 = 10
10 - 5 = 5
120 和 25 的最大公约数是:5
使用减法的欧几里得算法
120 - 25 = 95
95 - 25 = 70
70 - 25 = 45
45 - 25 = 20
25 - 20 = 5
20 - 5 = 15
15 - 5 = 10
10 - 5 = 5
120 和 25 的最大公约数是:5
使用减法的欧几里得算法
120 - 25 = 95
95 - 25 = 70
70 - 25 = 45
45 - 25 = 20
25 - 20 = 5
20 - 5 = 15
15 - 5 = 10
10 - 5 = 5
120 和 25 的最大公约数是:5