Wed May 10 15:31:00 UTC 2023 inscode

上级 6ddbff46
class Main { /*
public static void main(String[] args) { 欧几里得算法:辗转求余
System.out.println("Hello world!"); 原理: gcd(a,b)=gcd(b,a mod b)
当b为0时,两数的最大公约数即为a
} getchar()会接受前一个scanf的回车符
} */
\ No newline at end of file #include<stdio.h>
// 辗转相除算法求最大公约数
unsigned int MaxCommonFactor(int a,int b)
{
if(a % b == 0) // 若a能够被b整除,则返回b
return b;
return MaxCommonFactor(b, a % b); // 否则继续递归
}
// 更相减损术算法求最大公约数
unsigned int Gcd(unsigned int M,unsigned int N)
{
unsigned int Rem;
while(N > 0) // 当N不为0时,进行循环
{
Rem = M % N; // 计算余数
M = N; // 交换M和N的值
N = Rem;
}
return M; // 返回最大公约数
}
int main(void)
{
int a,b;
scanf("%d %d",&a,&b); // 输入两个整数
printf("the greatest common factor of %d and %d is ",a,b);
printf("%d\n",Gcd(a,b)); // 输出最大公约数
printf("recursion:%d\n",MaxCommonFactor(a,b)); // 输出使用递归方法计算的最大公约数
return 0;
}
/*
"MaxCommonFactor"是一个数学术语,也称为最大公因数。
在两个或多个整数中,最大公因数是能够同时整除它们的最大正整数。
例如,12和18的最大公因数是6,因为6可以同时被12和18整除。
最大公约数 (greatest common divisor)
*/
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册