Math: GCD greatest common divisor 最大公约数()-其他
Math: GCD greatest common divisor 最大公约数()
Loop:
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
int m, n, r;
scanf("%d%d", &m, &n);
if (m < n) {
m = m ^ n;
n = m ^ n;
m = m ^ n;
}
while (n) {
r = m % n;
m = n;
n = r;
}
printf("great common divisor %d\n", m);
return 0;
}
Recursion:
package org.example;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in);
int a, b;
a = scanner.nextInt();
b = scanner.nextInt();
System.out.println(gcd(a, b));
}
public static int gcd(int x, int y) {
int t;
if (x < y) {
x = x ^ y;
y = x ^ y;
x = x ^ y;
}
if (x % y == 0) {
return y;
} else {
return gcd(y, x % y);
}
}
}
————————
Loop:
#include <stdio.h>
int main(void) {
printf("Hello, World!\n");
int m, n, r;
scanf("%d%d", &m, &n);
if (m < n) {
m = m ^ n;
n = m ^ n;
m = m ^ n;
}
while (n) {
r = m % n;
m = n;
n = r;
}
printf("great common divisor %d\n", m);
return 0;
}
Recursion:
package org.example;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
Scanner scanner = new Scanner(System.in);
int a, b;
a = scanner.nextInt();
b = scanner.nextInt();
System.out.println(gcd(a, b));
}
public static int gcd(int x, int y) {
int t;
if (x < y) {
x = x ^ y;
y = x ^ y;
x = x ^ y;
}
if (x % y == 0) {
return y;
} else {
return gcd(y, x % y);
}
}
}